【问题标题】:Gcc error when using ncurses library in a C program [duplicate]在 C 程序中使用 ncurses 库时出现 Gcc 错误 [重复]
【发布时间】:2021-04-27 09:06:30
【问题描述】:

我第一次在一个小测试程序中尝试 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 并执行 gcc -Wall -lncurses -o tests test.cgcc -Wall -c tests.c 然后 gcc tests.o -lncurses -o tests (我自己没有尝试过,我现在没有我的 linux 计算机,如果这可行,请告诉我)
  • 不幸的是,@KamilCuk 和 @Dzious 的两个建议都给了我另一个错误:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: undefined reference to symbol 'stdscr' /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /lib64/libtinfo.so.6: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status
  • gcc tests.o -o tests -lncurses -lcurses?参数的顺序很重要。你跑gcc -lncurses tests.o gcc tests.o -lncurses

标签: c linux gcc ncurses


【解决方案1】:

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

gcc tests.o -o tests -lncurses

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2021-08-29
    • 2019-02-21
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多