程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 C 程序中使用 ncurses 库时出现 Gcc 错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 C 程序中使用 ncurses 库时出现 Gcc 错误?

开发过程中遇到在 C 程序中使用 ncurses 库时出现 Gcc 错误的问题如何解决?下面主要结合日常开发的经验,给出你关于在 C 程序中使用 ncurses 库时出现 Gcc 错误的解决方法建议,希望对你解决在 C 程序中使用 ncurses 库时出现 Gcc 错误有所启发或帮助;

我在一个小测试程序中第一次尝试 ncurses C 库,每次我尝试使用 gcc 输出可执行文件时,它都会给我一个错误。

这是测试程序:

#include <ncurses.h>

int main() {
    initscr();

    printw("--------\n| test |\n--------\n\n");
    refresh();

    printw("\npress any key to exit...");
    refresh();

    getch();
    
    enDWin();
    return 0;
}

以下是 gcc 命令及其输出:

$ gcc -Wall -lncurses -c tests.c
$ gcc tests.o -o tests
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: warning: reLOCATIOn against `stdscr' in read-only section `.text'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: in function `main':
tests.c:(.text+0x5): undefined reference to `initscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x16): undefined reference to `printw'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x1d): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x25): undefined reference to `wrefresh'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x36): undefined reference to `printw'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x3d): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x45): undefined reference to `wrefresh'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x4C): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x54): undefined reference to `wgetch'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x59): undefined reference to `enDWin'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creaTing DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

我试图找到解决方案,但没有找到,因此感谢您提供任何帮助。

注意:我使用没有 X 客户端的 Wayland 显示服务器这一事实与错误有关吗?

解决方法

必须在命令行的末尾添加这些库,因为它们只会被搜索一次符号,并且链接是按照指定的顺序完成的。

gcc tests.o -o tests -lncurses

大佬总结

以上是大佬教程为你收集整理的在 C 程序中使用 ncurses 库时出现 Gcc 错误全部内容,希望文章能够帮你解决在 C 程序中使用 ncurses 库时出现 Gcc 错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。