Undefined reference to `initscr' Ncurses - c

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

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.

In C++ , I fixed it just by linking the ncurses library .
Here is the command :
g++ main.cpp -lncurses

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)

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.

Related

Unable to make shared library while embedding Python to C

I am trying to make a shared library which consists of one C header file and two C source files, one of which calls a Python file for implementation.
The compilation comand used is
gcc -fPIC -c -I-I/usr/local/include -I/usr/local/include -I/usr/local/include/python3.4m -I/usr/local/include/python3.4m -DNDEBUG -g -fwrapv -O0 -Wall -Wstrict-prototypes -DDOUBLE_PRECISION *.c
I am able to compile all these without any error, but when I try to make a shared object file using the following command
gcc *.o -L/usr/local/lib -lpthread -ldl -lutil -lm -Xlinker -export-dynamic /usr/local/lib/python3.4/config-3.4m/libpython3.4m.a -shared -o libroughness.so
I get the following error which I am unable to resolve
/usr/bin/ld: /usr/local/lib/python3.4/config-3.4m/libpython3.4m.a(abstract.o): relocation R_X86_64_32S against `_PyObject_NextNotImplemented' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/python3.4/config-3.4m/libpython3.4m.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
I am using -fPIC tag while compilation, however it still asks me to recompile with fPIC. Any help on how to resolve this case would be appreciated.

Contiki port will not override printf

I am trying to create a port of contiki for my custom platform using an stm32l1 processor. However when i am trying to override the default printf function from arm-none-eabi-gcc newlib it will not let me.
I have tried creating hedaer and source files, adding them to be built and then either creating the function 'printf' and 'sprintf' in them or naming them something different and then trying to use
#define printf my_printf.
I have also followed other tips i found online that tells me to create a newlib stub file providing the various functions _sbrk , _write , _read... and so on.
However everytime i try to build my program with any printf command, the linker complains on all the newlib stub files with undefinded references.
undefined reference to _sbrk
How can i get the contiki build system to override the standard printf function?
On ubuntu14:10 with gcc 4.93.
CC targets/r1501/printf/printf-stdarg.c
CC contiki/../arm/stm32l152/./syscalls.c
CC ....
CC contiki/core/net/mac/cxmac/cxmac.c
AR contiki-r1501.a
CC project.c
LD project.r1501
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/thumb/libg.a(lib_a-sbrkr.o): In function_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to _sbrk'
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/thumb/libg.a(lib_a-writer.o): In function_write_r':
writer.c:(.text._write_r+0x10): undefined reference to _write'
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/thumb/libg.a(lib_a-closer.o): In function_close_r':
....More errors from libg.a `
In the syscalls.c file all of theese undefined references are specified.
and printf and sprintf are defined in printf-sdarg.c
arm-none-eabi-gcc -DCONTIKI=1 -DCONTIKI_TARGET_R1501=1
-DNETSTACK_CONF_WITH_IPV6=1 -DUIP_CONF_IPV6_RPL=1 -DAUTOSTART_ENABLE -DHSE_VALUE=8000000 -DF_CPU=32000000 -DPLL_SOURCE_HSE -D'GIT_DESC="v0.1-23-g0c3a4d8-dirty"' -specs=nosys.specs -I. -Icontiki/core -Icontiki/../arm/stm32l152 -Icontiki/../arm/stm32l152/loader -Icontiki/../arm/stm32l152/lib/STM32L1xx_StdPeriph_Driver/inc -Icontiki/../arm/stm32l152/lib/CMSIS/Include -Icontiki/../arm/stm32l152 -Icontiki/platform/r1501 -DWITH_ASCII -DMCK=32000000 -mlittle-endian -mthumb -mcpu=cortex-m3 -msoft-float -Wno-strict-aliasing -DRUN_AS_SYSTEM -DROM_RUN -std=c99 -g -lc -larchive -Lcontiki/../arm/stm32l152 -fdiagnostics-color=always -O0 -I. -Itargets/r1501/. -Itargets/r1501/dev -Itargets/r1501/rf230bb -Itargets/r1501/printf -Icontiki/../arm/stm32l152/. -Icontiki/../arm/stm32l152/lib/STM32L1xx_StdPeriph_Driver/src -Icontiki/core/dev -Icontiki/core/lib -Icontiki/core/net -Icontiki/core/net/llsec -Icontiki/core/net/mac -Icontiki/core/net/rime -Icontiki/core/net/rpl -Icontiki/core/sys -Icontiki/core/cfs -Icontiki/core/ctk -Icontiki/core/lib/ctk -Icontiki/core/loader -Icontiki/core/. -Icontiki/apps/ping6 -Icontiki/core/sys -Icontiki/core/dev -Icontiki/core/lib -Icontiki/core/net/ipv6 -Icontiki/core/net/ip -Icontiki/core/net/rpl -Icontiki/core/net/mac -Icontiki/core/net/llsec -Icontiki/core/net/mac/sicslowmac -Icontiki/core/net/rime -Icontiki/core/net -Icontiki/core/net/mac/contikimac -Icontiki/core/net/mac/cxmac -Itargets/r1501/ -Icontiki -DCONTIKI_VERSION_STRING=\"Contiki-2.6-2077-ga79edc6\" -DAUTOSTART_ENABLE -c project.c -o project.co arm-none-eabi-gcc -L contiki/../arm/stm32l152 -T contiki/../arm/stm32l152/stm32l1xx.ld -g
-nostartfiles -mthumb -fdiagnostics-color=always project.co obj_r1501/contiki-main.o \
contiki-r1501.a -o project.r1501 /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/thumb/libg.a(lib_a-sbrkr.o): In function `_sbrk_r':

Linking Math Library in GCC 4.6.1 (Ubuntu 11.10)

I find a problem in the linking process of my application. I did not have the same with gcc 4.5. It tries to link math library with the following command.
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -lm -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals
and report following error massages:
undefined reference to `sqrt'
Any idea ?
recent gcc/ld uses the --as-needed linker flag as default. Practically, that means libraries have to be specified in the reverse order of dependencies on the command line. If the mems_internals library needs the sqrt function your -lm after -lmems_internals.
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals -lm
I've had the same problem with gcc 4.6.1, even with only one library. This doesn't work:
$ gcc -lm eg.o -o eg
eg.o: In function `foo':
/home/nick/tmp/eg.c:5: undefined reference to `sqrt'
collect2: ld returned 1 exit status
But this does:
$ gcc eg.o -o eg -lm
I hit this because I was using "LDFLAGS=-lm" in my Makefile. Works fine if you use "LDLIBS=-lm" instead.
You didn't tell us what -lmems_internals is, but maybe the unresolved symbol comes from there. The order of the -l options is generally important to the linker, you should always put system libraries last.
You can check where the unresolved symbol comes from by using something like
nm yourLibrary | grep sqrt
if there is a U in front of sqrt the symbol is undefined.
I'd say the linker is using the wrong libm.

Why am I getting a gcc "undefined reference" error trying to create shared objects?

Why am I getting an "undefined reference" error using gcc?
I am trying to create a shared object (.so) that exports one function, "external()". I then try to link against the .so but get "undefined reference 'external'". What am I doing wrong here?
File: external.c
int external() {
return 5;
}
File: program.c
int external();
int main(char** argv, int* argc) {
return external();
}
Commands:
$ gcc -fPIC -c external.c
$ gcc -shared -o libexternal.so external.o
$ gcc -L. -lexternal -o program program.c
/tmp/cc3MmhAE.o: In function `main':
program.c:(.text+0x7): undefined reference to `external'
collect2: ld returned 1 exit status
I can even run nm and see that the .so is defining 'external':
Command:
$ nm libexternal.so | grep external
0000040c T external
What am I missing here?
Recent versions of gcc/ld default to linking with --as-needed.
This means if you write -lexternal before the C file the library will automatically get excluded (the order matters when testing if things are "needed" like this)
You can fix this with either of:
gcc -L. -o program program.c -lexternal
gcc -L. -Wl,--no-as-needed -lexternal -o program program.c
The latter of which passes --no-as-needed to the linker, which would cause the library to still be linked, even if you didn't call external() from it.
Note: -Wl,--no-as-needed isn't applied globally to everything that's linked, it's only applied to things that follow it in the command line order. So -lexternal -Wl,--no-as-needed also wouldn't work. This does mean that you can mix and match behaviours though, for example gcc -L. -Wl,--no-as-needed -lexternal -Wl,--as-needed -o program program.c -lmightneed would always link against external, but only link against mightneed if one or both of program.c/libexternal.so caused it to be needed.

Linker error: undefined reference to shared object

I am trying to use an external library called iniparser in my C program. I'm using gcc 4.4.
I put the iniparser library in a subdirectory called lib/ header files are in lib/iniparser/src and the library is compiled to lib/iniparser/libiniparser.so.0.
I wrote a short Makefile to compile it, here's the output of make:
gcc -Wall -Wextra -Werror -c -I include/ src/smag_main.c -L lib/iniparser -liniparser -I lib/iniparser/src
gcc -Wall -Wextra -Werror -c -I include/ -L lib/iniparser -liniparser -I lib/iniparser/src src/agros.c
gcc -Wall -Wextra -Werror -c -I include/ -L lib/iniparser -liniparser -I lib/iniparser/src src/main.c
gcc -Wall -Wextra -Werror -L lib/iniparser -liniparser -o agros smag_main.o main.o agros.o
smag_main.o: In function `sec_haskey':
smag_main.c:(.text+0xa9): undefined reference to `iniparser_find_entry'
smag_main.o: In function `parse_config':
smag_main.c:(.text+0x153): undefined reference to `iniparser_load'
smag_main.c:(.text+0x18b): undefined reference to `iniparser_getint'
smag_main.c:(.text+0x1c6): undefined reference to `iniparser_getstring'
smag_main.c:(.text+0x202): undefined reference to `iniparser_getstring'
smag_main.c:(.text+0x261): undefined reference to `iniparser_getstring'
smag_main.c:(.text+0x2c2): undefined reference to `iniparser_getint'
smag_main.c:(.text+0x2d5): undefined reference to `iniparser_freedict'
collect2: ld returned 1 exit status
make: *** [agros] Error 1
First call to gcc compiles smag_main.o successfully, the second one compiles agros.o and the third one main.o. The 4th call is the linker, that will link all those objects into an executable agros. It obviously fails.
It looks like it has problems locating iniparser.so at linking time. How's my call wrong?
I am confused.
(Alternate question, if anyone could explain how to the linking by calling ld directly it would be great).
Try putting a symlink from libiniparser.so.0 to libiniparser.so
cd lib/iniparser/
ln -s libiniparser.so.0 libiniparser.so

Resources