Reputation: 41945
I installed a new lib from source and I am trying to link to it. The linker complains that it cannot find the symbols that should be in the lib.
Here are the errors I get:
$ make all
gcc -std=c99 -g -Wall -O3 `pkg-config --cflags --libs libmodbus` -c client.c -o client.o
gcc -std=c99 -g -Wall -O3 `pkg-config --cflags --libs libmodbus` client.o -o client
client.o: In function `main':
/home/gauthier/code/modbus/client.c:29: undefined reference to `modbus_new_tcp'
/home/gauthier/code/modbus/client.c:30: undefined reference to `modbus_connect'
/home/gauthier/code/modbus/client.c:33: undefined reference to `modbus_read_registers'
/home/gauthier/code/modbus/client.c:35: undefined reference to `modbus_close'
/home/gauthier/code/modbus/client.c:36: undefined reference to `modbus_free'
collect2: error: ld returned 1 exit status
make: *** [client] Error 1
pkg-config
says this:
$ pkg-config --cflags --libs libmodbus
-I/usr/local/include/modbus -L/usr/local/lib -lmodbus
and the files in /usr/local/lib
are these:
$ ls /usr/local/lib/ | grep mod
libmodbus.la
libmodbus.so
libmodbus.so.5
libmodbus.so.5.0.5
Also, the so file contains the undefined references:
$ grep modbus_connect /usr/local/lib/*
Binary file /usr/local/lib/libmodbus.so matches
Binary file /usr/local/lib/libmodbus.so.5 matches
Binary file /usr/local/lib/libmodbus.so.5.0.5 matches
I tried adding /usr/local/lib
to LD_LIBRARY_PATH
, to no av. Not that I though it would help since I have -L
in my compiler command anyway.
I also tried to manually write the -I
, -L
, and -l
(not using pkg-config
), that didn't help.
How do I make the linker understand that the references to the modbus_*
functions are right there?
Upvotes: 1
Views: 1724
Reputation:
Let me guess.
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.
http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
So try put pkg-config --cflags --libs libmodbus
at the end
Upvotes: 5