Linux gcc /usr/bin/ld: 错误:找不到 -lncurses
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14416487/
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
gcc /usr/bin/ld: error: cannot find -lncurses
提问by Yrrej10
I am running Ubuntu 12.04 and I'm currently working on a project involving C, OpenGL, a teapot and input methods.
我正在运行 Ubuntu 12.04,目前正在从事一个涉及 C、OpenGL、茶壶和输入法的项目。
The problem started when I decided to have arrow keys as input. I checked to see the key codes for arrow keys but all of the arrows return 0. I looked up how to get this to work and I found conio.h. Unfortunately, it is an old DOS header that is not available for Linux. Then I found a substitute called ncurses.
当我决定将箭头键作为输入时,问题就开始了。我检查了箭头键的键码,但所有的箭头都返回 0。我查找了如何让它工作,我找到了 conio.h。不幸的是,它是一个旧的 DOS 标头,不适用于 Linux。然后我找到了一个叫做 ncurses 的替代品。
After installing the necessary libraries, by following the build instructions closely, I #included curses.h in my main.c source. When I first tried to compile using gcc, I got the following errors:
安装必要的库后,通过密切遵循构建说明,我在 main.c 源代码中#include了curses.h。当我第一次尝试使用 gcc 进行编译时,出现以下错误:
main.o:main.c:function _Key: error: undefined reference to 'stdscr'
main.o:main.c:function _Key: error: undefined reference to 'wgetch'
main.o:main.c:function _Key: error: undefined reference to 'stdscr'
main.o:main.c:function _Key: error: undefined reference to 'wgetch'
I found a fix by adding -lncurses to the makefile like so:
我通过将 -lncurses 添加到 makefile 中找到了解决方法,如下所示:
SOURCES=main.c
main: main.o
gcc -lm -lGL -lGLU -lglut -lncurses main.o -o main
main.o: main.c
gcc -lm -lGL -lGLU -lglut -c main.c
But I was greeted by another error:
但是我遇到了另一个错误:
/usr/bin/ld: error: cannot find -lncurses
As well as the previous errors.
以及之前的错误。
I have spent the last 2 days searching both the Ubuntu forums and StackOverFlow. Any help would be appreciated.
过去 2 天我一直在搜索 Ubuntu 论坛和 StackOverFlow。任何帮助,将不胜感激。
P.S. I don't know if this is important but when I try to run /usr/bin/ld I get this error:
PS 我不知道这是否重要,但是当我尝试运行 /usr/bin/ld 时出现此错误:
ld: fatal error: no input files
回答by Some programmer dude
First off, you should put the libraries afterthe object file when linking. And not have them at all in the compilation of of the source file.
首先,您应该在链接时将库放在目标文件之后。并且在源文件的编译中根本没有它们。
After that, if ncurses is not installed in a standard search folder you need to point out to the linker where it is, this is done with the -L
command line option:
之后,如果 ncurses 未安装在标准搜索文件夹中,您需要指出它所在的链接器,这是通过-L
命令行选项完成的:
gcc main.o -o main -L/location/of/ncurses -lm -lGL -lGLU -lglut -lncurses
回答by askmish
Try installing the ncurses-static
package too, if you have only the ncurses-devel
package installed in your Ubuntu OS.
ncurses-static
如果您ncurses-devel
的 Ubuntu 操作系统中只安装了该软件包,请尝试安装该软件包。
If that solves your problem, plus if you add @Joachim's compiling instructions, you are off to a great start.
如果这解决了您的问题,并且如果您添加了@Joachim 的编译说明,那么您就有了一个良好的开端。
gcc main.o -o main -L/location/of/ncurses -lm -lGL -lGLU -lglut -lncurses
The linker can't find your shared library in it's search path. If you add the directory where your shared lib is to the LD_LIBRARY_PATH
environment variable the linker should find it and be able to link against it. In that case you could omit the -L
option to gcc:
链接器在其搜索路径中找不到您的共享库。如果将共享库所在的目录添加到LD_LIBRARY_PATH
环境变量中,链接器应该会找到它并能够链接到它。在这种情况下,您可以省略-L
gcc的选项:
gcc main.o -o main -lm -lGL -lGLU -lglut -lncurses
And it should compile fine.
它应该可以正常编译。
EDIT:
Good to know that apt-get install libncurses5-dev
fixes your problem.
编辑:很高兴知道可以apt-get install libncurses5-dev
解决您的问题。
FYI.
The LD_LIBRARY_PATH
environment variable contains a colon separated list of paths that the linker uses to resolve library dependencies at run time. These paths will be given priority over the standard library paths /lib
and /usr/lib
. The standard paths will still be searched, but only after the list of paths in LD_LIBRARY_PATH
has been exhausted.
供参考。该LD_LIBRARY_PATH
环境变量包含的路径,冒号分隔列表的链接器使用来解决在运行时库的依赖。这些路径将优先于标准库路径/lib
和/usr/lib
. 标准路径仍然会被搜索,但只有在路径列表LD_LIBRARY_PATH
已经用完之后。
The best way to use LD_LIBRARY_PATH
is to set it on the command line or script immediately before executing the program. This way you can keep the new LD_LIBRARY_PATH
isolated from the rest of your system i.e. local to the current running running instance of shell.
最好的使用方法LD_LIBRARY_PATH
是在执行程序之前立即在命令行或脚本中设置它。通过这种方式,您可以将新的LD_LIBRARY_PATH
与系统的其余部分隔离,即本地到当前运行的 shell 实例。
$ export LD_LIBRARY_PATH="/path/to/libncurses/library/directory/:$LD_LIBRARY_PATH"
$ gcc main.o -o main -lm -lGL -lGLU -lglut -lncurses
回答by Dale
For anyone with the same problem I had: I was missing the 32 bit libraries; I was compiling 32 bit on a 64 bit server which was missing the lib32ncurses5-dev package.
对于遇到同样问题的任何人:我缺少 32 位库;我在缺少 lib32ncurses5-dev 包的 64 位服务器上编译 32 位。
On Ubuntu I simply ran:
在 Ubuntu 上,我简单地运行:
sudo apt-get install lib32ncurses5-dev