Linker Error : Statically Linking of Boost Serialization Library - c++

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

Related

wxWidgets minimal.cpp liner error in function `wxString::ImplStr(char const*, wxMBConv const&)'

Getting this error when compiling and linking the minimal.cpp file from the samples directory
g++ -o "SampleMinimal" ./src/minimal.o -lwx_mswu_xrc-3.0-x86_64-w64-mingw32 -lwx_mswu_webview-3.0-x86_64-w64-mingw32 -lwx_mswu_html-3.0-x86_64-w64-mingw32 -lwx_mswu_qa-3.0-x86_64-w64-mingw32 -lwx_mswu_adv-3.0-x86_64-w64-mingw32 -lwx_mswu_core-3.0-x86_64-w64-mingw32 -lwx_baseu_xml-3.0-x86_64-w64-mingw32 -lwx_baseu_net-3.0-x86_64-w64-mingw32 -lwx_baseu-3.0-x86_64-w64-mingw32 -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -Wl,--subsystem,windows -mwindows -Wl,--enable-auto-import
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: ./src/minimal.o: in function `wxString::ImplStr(char const*, wxMBConv const&)':
/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0/wx/string.h:477: undefined reference to `wxString::ConvertStr(char const*, unsigned long, wxMBConv const&)'
/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0/wx/string.h:477:(.text$_ZN8wxString7ImplStrEPKcRK8wxMBConv[_ZN8wxString7ImplStrEPKcRK8wxMBConv]+0x3a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `wxString::ConvertStr(char const*, unsigned long, wxMBConv const&)'
collect2: error: ld returned 1 exit status
Successfully creating object file.
Compiler command line:
g++ -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__ -I/usr/x86_64-w64-mingw32/sys-root/mingw/lib/wx/include/x86_64-w64-mingw32-msw-unicode-3.0 -I"/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/minimal.d" -MT"src/minimal.o" -o "src/minimal.o" "../src/minimal.cpp"
Finished building: ../src/minimal.cpp
Linker command line:
g++ -o "SampleMinimal" ./src/minimal.o -lwx_mswu_xrc-3.0-x86_64-w64-mingw32 -lwx_mswu_webview-3.0-x86_64-w64-mingw32 -lwx_mswu_html-3.0-x86_64-w64-mingw32 -lwx_mswu_qa-3.0-x86_64-w64-mingw32 -lwx_mswu_adv-3.0-x86_64-w64-mingw32 -lwx_mswu_core-3.0-x86_64-w64-mingw32 -lwx_baseu_xml-3.0-x86_64-w64-mingw32 -lwx_baseu_net-3.0-x86_64-w64-mingw32 -lwx_baseu-3.0-x86_64-w64-mingw32 -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -Wl,--subsystem,windows -mwindows -Wl,--enable-auto-import
You're apparently compiling minimal sample in debug mode, see -g3 flag, then attempting to link against release mswu libs.
So either change the sample compilation to release mode, or change the linker command to use mswud debug libs.
Solved the problem by making Command: x86_64-w64-mingw32-g++ for both the GCC C++ compiler and the MinGW C++ Linker in the C/C++ build settings in Eclipse. I thought this was done automatically by selecting the MinGW tool chain. Many thanks to Catalin for pointing out that I was linking against release libraries in the debug build configuration. His answer to make my build configuration release got me that one step further. Thanks!

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

How to link C++ Application with cppkafka

As mentioned in the cppkafka official GitHub in usage section.
If you want to use cppkafka, you'll need to link your application with:
cppkafka
rdkafka
I tried to compile example program mentioned in the README.md of the GitHub with
gcc -O3 -Wall test.cpp -std=c++11 -lrdkafka -lpthread -lz -lstdc++ -D_GLIBCXX_USE_CXX11_ABI=0 -lcppkafka
It is throwing with following error
test.cpp:(.text.startup+0x46): undefined reference to `cppkafka::ConfigurationOption::ConfigurationOption(std::string const&, char const*)'
test.cpp:(.text.startup+0xd9): undefined reference to `cppkafka::Buffer::Buffer(std::string const&)'
collect2: error: ld returned 1 exit status
No need of -D_GLIBCXX_USE_CXX11_ABI=0 option while compiling.
you can simply compile this as following
gcc -O3 -Wall test.cpp -std=c++11 -lrdkafka -lpthread -lz -lstdc++ -lcppkafka

undefined reference to boost library

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.

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.