gcc /usr/bin/ld: error: cannot find -lncurses

放肆的年华 提交于 2019-12-02 19:05:44

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.

On Ubuntu I simply ran:

sudo apt-get install lib32ncurses5-dev

First off, you should put the libraries after the 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:

gcc main.o -o main -L/location/of/ncurses -lm -lGL -lGLU -lglut -lncurses

Try installing the ncurses-static package too, if you have only the ncurses-devel package installed in your Ubuntu OS.

If that solves your problem, plus if you add @Joachim's compiling instructions, you are off to a great start.

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:

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.

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.

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.

$ export LD_LIBRARY_PATH="/path/to/libncurses/library/directory/:$LD_LIBRARY_PATH"
$ gcc main.o -o main -lm -lGL -lGLU -lglut -lncurses
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!