23

我正在尝试编译我的项目并使用 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

非常感谢

4

4 回答 4

40

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

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

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

于 2013-04-24T12:31:38.173 回答
6

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

这是命令:

g++ main.cpp -lncurses
于 2018-01-09T23:44:04.237 回答
2

通过使用 LDLIBS 变量,我得到了正确顺序的标志:

ifndef PKG_CONFIG
PKG_CONFIG=pkg-config
endif

CFLAGS+=-std=c99 -pedantic -Wall
LDLIBS=$(shell $(PKG_CONFIG) --libs ncurses)
于 2014-09-05T15:17:33.087 回答
0
man gcc | grep -A10 "\-l library"

-l 库

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

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

于 2017-06-19T08:58:10.660 回答