Compiling Dynamically Linked Library in C++ with Run Path Issue - c++

I'm busy trying to compile and link a c++ program using the following make file
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
but I'm getting the error
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
driver.so: In function `main':
driver.cpp:(.text+0x9): undefined reference to `tokenlib::acquire_token()'
collect2: error: ld returned 1 exit status
make: *** [driver.so] Error 1
and my file hierarchy is
/root
makefile
driver.cpp
/tokenlib
libtokenlib.so
I've been told that adding -Wl,-rpath=./tokenlib to my g++ arguements would solve the problem so I tried like this
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so -Wl,-rpath=./tokenlib
g++ -L/tokenlib/libtokenlib.so driver.so -o linked -Wl,-rpath=./tokenlib
but it still isn't working.
I'm a complete novice and any help would be appreciated.

The linker usually refers to .a (stub) libraries, not to .so files!
-L options specify the paths, the lnker should take in account.
The -l<mystuff> option tries to resolve a library named libmystuff.a from any of the given path's of the -L options, or compiler intrinsic library search paths.

Related

C++ file compiling: -L and -I arguments don't work for boost library

There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.

GCC not finding LibSerial even with -lserial flag [Ubuntu] [duplicate]

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.

compiling on friendlyarm qtopia error

after trying to cross compile a program for a week i gave up and now im trying to compile it directly on the friendlyarm running qtopia 2.2.0, but when i run make i am getting some strange errors. Can some one shine some more light on the and maybe point me in the right direction on how to fix the problem?
the make output:
/sdcard/images/makef # make
make: Warning: File `Makefile' has modification time 2.2e+04 s in the future
gcc -c -o obj/main.o main.c -I./
gcc -c -o obj/serial.o serial.c -I./
gcc -c -o obj/fb.o fb.c -I./
gcc -c -o obj/menu_main.o menu_main.c -I./
gcc -c -o obj/timer.o timer.c -I./
gcc -c -o obj/cmdin.o cmdin.c -I./
cmdin.c: In function 'processcmd':
cmdin.c:64: warning: format '%f' expects type 'float *', but argument 4 has type 'int *'
gcc -c -o obj/buzzer.o buzzer.c -I./
gcc -c -o obj/statemachine.o statemachine.c -I./
gcc -c -o obj/inout.o inout.c -I./
gcc -c -o obj/network.o network.c -I./
gcc -c -o obj/text_file_input.o text_file_input.c -I./
gcc -c -o obj/text_file_input_oven.o text_file_input_oven.c -I./
gcc -o main obj/main.o obj/serial.o obj/fb.o obj/menu_main.o obj/timer.o obj/cmdin.o obj/buzzer.o obj/statemachine.o obj/inout.o obj/network.o obj/text_file_input.o obj/text_file_input_oven.o -I./ -lgd -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.a when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.a when searching for -lrt
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_create.o): In function `timer_create':
timer_create.c:(.text+0xd4): undefined reference to `pthread_once'
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o): In function `timer_helper_thread':
timer_routines.c:(.text+0xfc): undefined reference to `pthread_create'
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o): In function `__start_helper_thread':
timer_routines.c:(.text+0x1a0): undefined reference to `pthread_attr_setstacksize'
timer_routines.c:(.text+0x1e4): undefined reference to `pthread_create'
timer_routines.c:(.text+0x228): undefined reference to `pthread_atfork'
collect2: ld returned 1 exit status
make: *** [main] Error 1
/sdcard/images/makef #
Also how can i get if this message:
make: Warning: File `Makefile' has modification time 2.2e+04 s in the future
i tried
touch *.*
but that didn't help
make: Warning: File `Makefile' has modification time 2.2e+04 s in the
future
Your clocks on the system you are compiling on and the system that generated the files are out of sync. You should fix that (in another answer is mentioned a workaround using touch, but this is only a good idea if the "other computer" is running the wrong time - if your system you are compiling on is running the wrong time, then you should fix the time on the current system - ideally using ntp (network time protocol) to set your time on both systems from a network source at boot or similar - that way, you don't have to worry about them going out of phase either [a PC system clock will drift between 1 and 30 seconds in a month, depending on the actual hardware used].
/usr/bin/ld: skipping incompatible
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
These messages are harmless as long as the system is able to find some librt that "is compatible", and it seems like it does, since we get this.
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o)
The following indicates that libpthread is not being linked (in the right place)
timer_create.c:(.text+0xd4): undefined reference to `pthread_once'
You need to use -lpthread on your linker line - after -lrt, since librt is what is using pthread functions. Note that libraries are sensitive to order (and sometimes you even need to give the same library twice, because there are cyclic dependencies)
That message is usually an indication that some of your files have modification times later than the current system time. A possible solution is to "touch" every file in the source tree in order to update timestamps: go to the root of the subtree an do "find . -exec touch {} \; "
Then clean your project, delete all build files and retry compilation.
It seems that You have also forgotten to link libpthread. You should have posix libraries and link your project to libpthread.

I'm having linking or compilation errors

I am using Netbeans for my C++ project. I compiled my program using make and ran into this error:
collect2: error: ld terminated with signal 11 [Segmentation fault], core dumped
Makefile:4: recipe for target 'barn' failed
make: *** [barn] Error 1
Whereas, when I compiled it in a linux environment(Ubuntu to be precise), it compiled fine. What could have possibly gone wrong?
This is what I got when I typed make -n:
g++ -c main.cc
g++ -c Animal.cc
g++ -c Bird.cc
g++ -c Chicken.cc
g++ -c Cat.cc
g++ -c Pig.cc
g++ -o barn main.o Animal.o Bird.o Chicken.o Cat.o Pig.o Random.o
PS I prefer using Netbeans
A segment fault in the linker suggests a bug with that. This is what I would try if I were to run into this problem.
At the command line do
g++ -o barn main.cc Animal.cc Bird.cc Chicken.cc Cat.cc Pig.cc Random.cc
If that does not work, try variations like:
g++ -o barn main.cc Pig.cc Random.cc Animal.cc Bird.cc Chicken.cc Cat.cc
The order should not matter. This is just the kind of thing I would try with a mystery-meat problem like this.

/usr/bin/ld: cannot find

I created a .so file and put it in the location /opt/lib and added this path to LD_LIBRARY_PATH now after this when I try to compile my main program with the following command:
g++ -Wall -I/home/alwin/Development/Calculator/ main.cpp -lcalc -o calculator
I get the following error:
/usr/bin/ld: cannot find -lcalc
collect2: ld returned 1 exit status
Can someone help me with this.
I created the shared library using the code blocks IDE
Add -L/opt/lib to your compiler parameters, this makes the compiler and linker search that path for libcalc.so in that folder.
When you make the call to gcc it should say
g++ -Wall -I/home/alwin/Development/Calculator/ -L/opt/lib main.cpp -lcalc -o calculator
not -libcalc.so
I have a similar problem with auto-generated makes.
You can create a soft link from your compile directory to the library directory.
Then the library becomes "local".
cd /compile/directory
ln -s /path/to/libcalc.so libcalc.so
You need to add -L/opt/lib to tell ld to look there for shared objects.
#Alwin Doss
You should provide the -L option before -l. You would have done the other way round probably. Try this :)
export LDFLAGS=-L/path to lib/ this will solve the error