C语言 对 `initscr' Ncurses 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16192087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to `initscr' Ncurses
提问by BoilingLime
I'm trying to compile my project and I use the lib ncurse. And I've got some errors when compiler links files.
我正在尝试编译我的项目并使用 lib ncurse。当编译器链接文件时,我遇到了一些错误。
Here is my flags line in Makefile:
这是我在 Makefile 中的标志行:
-W -Wall -Werror -Wextra -lncurses
I've included ncurses.h
我已经包含了 ncurses.h
Some layouts :
一些布局:
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
And here are my erros :
这是我的错误:
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
Thanks a lot
非常感谢
回答by Paul R
You need to change your makefile so that the -lncursesdirective comes afteryour object code on the gcc command line, i.e. it needs to generate the command:
您需要更改您的 makefile,以便-lncurses指令出现在gcc 命令行上的目标代码之后,即它需要生成命令:
gcc -W -Wall -Werror -Wextra -I./Includes/. -o Sources/NCurses/ncurses_init.o -c Sources/NCurses/ncurses_init.c -lncurses
This is because object files and libraries are linked in order in a single pass.
这是因为目标文件和库在一次传递中按顺序链接。
回答by Natesh bhat
In C++ , I fixed it just by linking the ncurses library .
在 C++ 中,我只是通过链接 ncurses 库来修复它。
Here is the command :
这是命令:
g++ main.cpp -lncurses
回答by Ari Malinen
I got flags to correct order by using LDLIBS variable:
我通过使用 LDLIBS 变量获得了正确顺序的标志:
ifndef PKG_CONFIG
PKG_CONFIG=pkg-config
endif
CFLAGS+=-std=c99 -pedantic -Wall
LDLIBS=$(shell $(PKG_CONFIG) --libs ncurses)
回答by mihai
man gcc | grep -A10 "\-l library"
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
-l 库
链接时搜索名为 library 的库。(将库作为单独参数的第二种选择仅适用于 POSIX 合规性,不推荐使用。)
在命令中编写此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,foo.o -lz bar.o 在文件 foo.o 之后但在 bar.o 之前搜索库 z。如果 bar.o 引用 z 中的函数,则可能不会加载这些函数。

