undefined reference to boost library - c++

I am getting static link errors to boost's non header only library. Let's solve it together:
The final binary is supposed to be built using libraries(libbasemainif.a for example) separately built as the higher layers. One of those layers, uses boost::filesystem.
Have a look at the simplified vesrion of g++ command:
g++ /mylis/1.a /mylibs/2.a
-L/myboost/Linux-x86_64/lib64
-Wl,-rpath,/myboost/Linux-x86_64/lib64 -Wl,-Bstatic
-lboost_thread-mt -lboost_system-mt -lboost_filesystem-mt -lboost_date_time-mt
-Wl,-Bdynamic
-Wl,-rpath,/myinstall/usr/local/lib64 -L/myinstall/usr/local/lib64 -Wl,
-Bstatic -lmyblahblah-static -Wl,-Bdynamic **-lbasemainif** -lbaseif -ldl -rdynamic -lz -lrt
-L/mypackage1/Linux-x86_64/debug/lib -L /mypackage2/18.1/Linux-x86_64/debug/lib -lpthread -Wl,-rpath,$ORIGIN/../lib64
and this is the error :
/blahblah/lib/libbasemainif.a(errorreportfile.o):
In function `boost::filesystem3::remove(boost::filesystem3::path const&, boost::system::error_code&)':
/myboost/Linux-x86_64/include/boost/filesystem/v3/operations.hpp:411:
undefined reference to boost::filesystem3::detail::remove(boost::filesystem3::path const&, boost::system::error_code*)'
collect2: ld returned 1 exit status
If I remove -Wl,-Bstatic the g++ command executes successfully.But this is not an option in production.
The libboost_filesystem.a is available. And all of the laibraries in every layer used boost from the same location.
Can you please tell me why I am getting this error? Thank you

You should pass to gcc library that implements function after the piece which references it. Something like: -lbasemainif -lboost_filesystem-mt. This is how gcc linker resolves dependencies.
Here is great detailed explanation.

Related

Libnoise don't want to compile: Cannot find -lnoise

I'm trying to use the Libnoise library and I keep get this error:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -lnoise -static-libgcc-static-libstdc++ Resources/icon.res
f:/portableapps/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lnoise
collect2.exe: error: ld returned 1 exit status
I have the libnoise.lib in my Libs folder. I tried with -llibnoise instead, and I get:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -llibnoise -static-libgcc -static-libstdc++ Resources/icon.res
Compiled/Generator.o:Generator.cpp:(.text+0x3d): undefined reference to `noise::module::Perlin::GetValue(double, double, double) const'
Compiled/Generator.o:Generator.cpp:(.text+0x8a): undefined reference to `noise::module::Perlin::Perlin()'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0xa): undefined reference to `vtable for noise::module::Perlin'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0x19): undefined reference to `noise::module::Module::~Module()'
collect2.exe: error: ld returned 1 exit status
I don't know how to manually compile the source, it always return me a WinMain#16 error, no matter what I try to make.
Now, I really don't know what to do...
Anyone have a solution?
-l option of gcc supposes that library got name starting with lib, so -lnoise would link libnoise.
Usually .lib is a n import library or static library. The problems related to import libraries described here: How do import libraries work and why doesn't MinGW need them?
In relation to libnoise: Netbeans C++ using MinGW and the libnoise library

Linking caffe library and dependencies g++ make

I'm trying to add caffe to an existing project. I get 99 undefined reference errors, suggesting that it has something to do with linking libraries. The errors mention caffe, boost and google (relates to gflags or glog).
Linker command: (libcaffe is in /home/torcs/lib)
g++ main.o linuxspec.o -L/home/torcs/export/lib -lopenal -lalut -lvorbisfile -L/usr/lib -L/home/torcs/lib -lracescreens -lrobottools -lclient -lconfscreens -ltgf -ltgfclient -ltxml -lplibul -lraceengine -lmusicplayer -llearning -lplibjs -lplibssgaux -lplibssg -lplibsm -lplibsl -lplibsg -lplibul -lglut -lGLU -lGL -lpng -lz -ldl -lXrandr -lXrender -lXxf86vm -lXmu -lXi -lXt -lSM -lICE -lXext -lX11 -lm -lcaffe -lglog -o torcs-bin
Linker error (first 2 and final 2):
/home/torcs/export/lib/librobottools.so: undefined reference to `caffe::FillerParameter::_default_type_'
/home/torcs/export/lib/librobottools.so: undefined reference to `void caffe::caffe_gpu_set<float>(int, float, float*)'
...
/home/torcs/export/lib/librobottools.so: undefined reference to `caffe::NetStateRule::NetStateRule()'
/home/torcs/export/lib/librobottools.so: undefined reference to `caffe::Timer::MilliSeconds()'
collect2: error: ld returned 1 exit status
I added -lcaffe and tried libcaffe.a and libcaffe.so separately but the number of errors is not decreasing as I add libraries. If I misspel -lcaff it says cannot find libcaffe, which it doesn't do otherwise, so the lib is included correctly I guess.
I read up on linker order (windows background) and found that the symbols only get added if they are on the required symbols list. More specific libs should be last, dependent libs should be first. I figured that if I only add lcaffe at the end, the unrecognized symbol list should be populated, and at least the caffe symbols would be recognized, and maybe replaced by other dependencies. But that doesn't happen.
I tried to find whether libcaffe supplies the references. For the final undefined reference (caffe::Timer::MilliSeconds()), nm libcaffe.a finds:
0000000000188090 T _ZN5caffe5Timer12MilliSecondsEv
0000000000187910 T _ZN5caffe8CPUTimer12MilliSecondsEv
Which suggest the reference is in the lib. But the undefined reference error doesn't go away.
I also tried adding boost libs and glog, didn't help.
Edit. There is some additional weird behaviour going on. I think it is unrelated, but am not sure. The first time I build after a make clean I get an error about include <random>:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
This prevents the o files and librobottools.so from getting built. But if I make again, the c++11 error disappears, this lib does get build, and I see the 99 undefined reference errors.

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

Error while executing make command to create moses\scripts\training\memscore

I have to implement the machine translation system hence I am planning use moses but I facing following error while executing make command on Cygwin:
Administrator#diebold-69b7050 /cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore$ make
make all-am
make[1]: Entering directory `/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses
scripts/training/memscore'
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o -lz -lm
phrasetable.o: In function `_ZlsRSoRK15PhraseAlignment':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/phrasetable.cpp:111: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `__tcf_0':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/datastorage.h:31: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZN14PhrasePairInfo12realloc_dataEj':
/usr/include/boost/pool/simple_segregated_storage.hpp:97: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZNK14PhrasePairInfo14get_alignmentsEv':
Please don't give me suggestion like linker error because I am completely fad up of trying linker option.
I think, I have some Cygwin->Boot library problem. Can you suggest me where I am wrong?
You are mssing -lboost_system on your compilation commands.
Some of the boost libraries are header only. Others need to be compiled. And the libraries sometimes depend on each other. In this case you are using some boost library which needs -lboost_system. Add it so that it gets linked with your project.
and it should be on this line. Where linking is done
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o **-lboost_system** -lz -lm

Linker Error : Statically Linking of Boost Serialization Library

I'm trying to link the Boost Serialization Library to my Code. But it doesn't seem to be working.
g++ serialize.cpp -L"/usr/local/lib/libboost_serialization.a"
Error :
/tmp/ccw7eX4A.o: In function boost::archive::text_oarchive::text_oarchive(std::basic_ostream<char, std::char_traits<char> >&, unsigned int)':
serializep.cpp:(.text._ZN5boost7archive13text_oarchiveC2ERSoj[_ZN5boost7archive13text_oarchiveC5ERSoj]+0x25):
undefined reference toboost::archive::text_oarchive_impl::text_oarchive_impl(std::basic_ostream >&, unsigned int)'
..........
collect2: ld returned 1 exit status
But when i link as a shared library,
g++ serialize.cpp -lboost_serialization , it works fine.
What am i missing here
P.S : Other StackOverflow posts with the same question has no answers that work for the above error
g++ serialize.cpp -L"/usr/local/lib/libboost_serialization.a"
This command line is totally wrong: the -L flag tells the linker where to look for libraries; it does not tell the linker to use a library you are giving. Try this instead:
g++ serialize.cpp /usr/local/lib/libboost_serialization.a
Since I can't reproduce your problem, the better I can do is to tell you how I generally compile against boost_serialization statically:
g++ myapp.cpp -o myapp -Wall -static -static-libgcc -I/usr/local/include -L/usr/local/lib -lboost_serialization -lpthread