【问题标题】:Undefined reference to `initscr' Ncurses对“initscr”Ncurses 的未定义引用
【发布时间】:2023-03-30 04:03:01
【问题描述】:

我正在尝试编译我的项目并使用 lib ncurse。当编译器链接文件时,我遇到了一些错误。

这是我在 Makefile 中的标志行:

-W -Wall -Werror -Wextra -lncurses

我已经包含了 ncurses.h

一些布局:

prompt$> dpkg -S curses.h
libslang2-dev:amd64: /usr/include/slcurses.h
libncurses5-dev: /usr/include/ncurses.h
libncurses5-dev: /usr/include/curses.h

prompt$> dpkg -L libncurses5-dev | grep .so
/usr/lib/x86_64-linux-gnu/libncurses.so
/usr/lib/x86_64-linux-gnu/libcurses.so
/usr/lib/x86_64-linux-gnu/libmenu.so
/usr/lib/x86_64-linux-gnu/libform.so
/usr/lib/x86_64-linux-gnu/libpanel.s

这是我的错误:

gcc -W -Wall -Werror -Wextra -I./Includes/. -lncurses -o Sources/NCurses/ncurses_init.o -c Sources/NCurses/ncurses_init.c
./Sources/NCurses/ncurses_init.o: In function `ncruses_destroy':
ncurses_init.c:(.text+0x5): undefined reference to `endwin'
./Sources/NCurses/ncurses_init.o: In function `ncurses_write_line':
ncurses_init.c:(.text+0xc5): undefined reference to `mvwprintw'
./Sources/NCurses/ncurses_init.o: In function `ncurses_init':
ncurses_init.c:(.text+0xee): undefined reference to `initscr'
collect2: error: ld returned 1 exit status

非常感谢

【问题讨论】:

标签: c compilation linker ncurses undefined-reference


【解决方案1】:

您需要更改您的 makefile,以便 -lncurses 指令出现在 gcc 命令行上的目标代码之后,即它需要生成命令:

gcc -W -Wall -Werror -Wextra -I./Includes/. -o Sources/NCurses/ncurses_init.o -c Sources/NCurses/ncurses_init.c -lncurses

这是因为目标文件和库在一次传递中按顺序链接。

【讨论】:

  • 请注意,这表明显示您的“Makefile 中的标志”是不够的,因为您的问题实际上并没有表现出问题。专家必须从经验中直觉。
  • 你如何使用 Makefile 来做到这一点?
  • @CpILL:如果您有新问题,请点击 按钮提问。如果有助于提供上下文,请包含指向此问题的链接。
【解决方案2】:

我通过使用 LDLIBS 变量获得了更正顺序的标志:

ifndef PKG_CONFIG
PKG_CONFIG=pkg-config
endif

CFLAGS+=-std=c99 -pedantic -Wall
LDLIBS=$(shell $(PKG_CONFIG) --libs ncurses)

【讨论】:

    【解决方案3】:
    man gcc | grep -A10 "\-l library"
    

    -l 库

    链接时搜索名为 library 的库。 (将库作为单独参数的第二种选择仅适用于 POSIX 合规性,不推荐。)

    在命令的哪个位置编写此选项会有所不同;链接器搜索和处理库和目标文件 按照它们指定的顺序。因此, foo.o -lz bar.o 搜索 文件 foo.o 之后的库 z 但是 bar.o 之前如果 bar.o 引用 z 中的函数,则可能不会加载这些函数。

    【讨论】:

      【解决方案4】:

      在 C++ 中,我只是通过链接 ncurses 库来修复它。

      命令如下:

      g++ main.cpp -lncurses
      

      【讨论】:

      • -lncurses 标志也解决了我在使用 gcc 编译时遇到的问题。
      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 2012-03-21
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多