Undefined reference to `initscr' Ncurses
Asked Answered
V

4

27

I'm trying to compile my project and I use the lib ncurse. And I've got some errors when compiler links files.

Here is my flags line in Makefile:

-W -Wall -Werror -Wextra -lncurses

I've included 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

Veinlet answered 24/4, 2013 at 12:29 Comment(2)
possible duplicate of GCC: Use OpenSSL's SHA256 Functions. And many, many more...Frere
And you learn the hard way that -l<lib> is not a flag, but rather a directive ;)Lawful
C
45

You need to change your makefile so that the -lncurses directive comes after your object code on the gcc command line, i.e. it needs to generate the command:

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.

Conti answered 24/4, 2013 at 12:31 Comment(3)
Note that this demonstrates showing your "flags in Makefile" was insufficient, since your question actually doesn't exhibit the problem. An expert had to intuit it from experience.Boy
how do you do this with a Makefile?Hankypanky
@CpILL: If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.Conti
S
6

In C++ , I fixed it just by linking the ncurses library .

Here is the command :

g++ main.cpp -lncurses
Slenderize answered 9/1, 2018 at 23:44 Comment(1)
the -lncurses flag also solved my issue when compiling with gcc.Bouilli
R
2

I got flags to correct order by using LDLIBS variable:

ifndef PKG_CONFIG
PKG_CONFIG=pkg-config
endif

CFLAGS+=-std=c99 -pedantic -Wall
LDLIBS=$(shell $(PKG_CONFIG) --libs ncurses)
Riven answered 5/9, 2014 at 15:17 Comment(0)
A
0
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.

Actaeon answered 19/6, 2017 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.