Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am running Ubuntu 12.04 and I'm currently working on a project involving C, OpenGL, a teapot and input methods.

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.

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.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:

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.

P.S. I don't know if this is important but when I try to run /usr/bin/ld I get this error:

ld: fatal error: no input files
like image 539
Yrrej10 Avatar asked Jan 19 '13 16:01

Yrrej10


People also ask

How do you resolve usr bin ld cannot find?

To resolve this problem, you should either provide the library file ( lib{nameOfTheLibrary}. so ) in those search paths or use -L command option. -L{path} tells the g++ (actually ld ) to find library files in path {path} in addition to default paths.

What does usr bin Ld Cannot find mean?

A message such as /usr/bin/ld: cannot find -linput actually means it was looking for a file named libinput.so . The -l flag is a command-line argument (to ld or to gcc ) that expects the library name to follow and then the library name is used to form the file name which includes the lib prefix and the .

What is Ldflags in Makefile?

LDFLAGS: Extra flags to give to compilers when they are supposed to invoke the linker, 'ld', such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead. LDLIBS: Library flags or names given to compilers when they are supposed to invoke the linker, 'ld'.


3 Answers

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 
like image 166
Dale Avatar answered Sep 27 '22 22:09

Dale


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 
like image 38
Some programmer dude Avatar answered Sep 27 '22 22:09

Some programmer dude


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
like image 26
askmish Avatar answered Sep 27 '22 20:09

askmish