Boost library issues - c++

I am trying to compile a simple c++ program using g++ which contains boost includes but i am getting the following error. I installed it from the tar file found in the boost site. I get the following error only with the asio library.
$ g++ -std=c++03 -Wall -pedantic -g -O2 tcp.cpp -lboost_system -lboost_date_time -lboost_thread -lboost_asio
/usr/bin/ld: cannot find -lboost_asio
collect2: error: ld returned 1 exit status

Boost Asio is header only.
Drop -lboost_asio

ASIO doesn't have a library built for it, so indicating -lboost_asio as an option doesn't really make sense.
However, ASIO does have a dependency on boost.system, which does require a library be built for it.
Make sure you've properly built the boost library in the first place (correct optimization flags, correct Runtime-Library linking, correct architecture, and so on), and that the generated library files (the .so files, in your case) are accessible to the executable.

Related

undefined reference to `shm_open' already with -L /lib -lrt -lpthread

I just want to use the boost library to create a shared memory on an ARM system. It work fine if you want to compile it only under ubuntu. However, when I want to cross compile it with TI's CCSv6 and angstrom toolchain, it keep pushing errors.
Because I do not know how to write a makefile for cross compile, I think using TI their own IDE might be a good choice to avoid further problems.
Here is my code and print out of build console.
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
int main()
{
shared_memory_object shdmem{open_or_create, "Boost1", read_write};
shdmem.truncate(1024);
mapped_region region{shdmem, read_write};
}
g++ -std=c++0x -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -L /lib -lrt -lpthread -fPIC
The IDE called Code Composer Studio has cross compile settings as below:
Prefix: arm-angstrom-linux-gnueabi-
Path: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/armv5te-angstrom-linux-gnueabi
Build Console:
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference toshm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference toshm_open'
collect2: ld returned 1 exit status
make: *** [test] Error 1
undefined reference to shm_open' means it cannot find -lrt for ARM.
In your build command line you need to specify include and library paths to ARM built libraries, not to Ubuntu ones. So -I/usr/include and -L /lib is wrong.
Also you need boost built for ARM, although if you just want to use interprocess library then boost headers should be enough. But you need to copy them into different location because including them from /usr/include includes also other headers specific to Ubuntu.
You can use the cross compiler IDE you mentioned or arm g++ cross compiler which you can install by:
sudo apt-get install g++-arm-linux-gnueabihf. Some headers and libraries for ARM will be installed too.

Need some help figuring out compile-time error: 'Undefined symbols for architecture x86_64: "boost::system::system_category()"' [duplicate]

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++

compiling boost.asio using g++ compiler

I'm new to g++ compiler and also trying to start coding with boost.asio libraries, but have some difficulties compiling the code using g++.
I have installed boost libraries usingsudo apt-get install libboost-all-dev and boost folder is located in /usr/include directory, and whole scenario:
geek#ubuntu:~/workspace/HelloBoost/src$ g++ -I /usr/include/ -l boost_asio HelloBoost.cpp
/usr/bin/ld: cannot find -lboost_asio
collect2: ld returned 1 exit status
Thanks for your answers.
Did you read about using boost.asio ? Apparently, it looks like a headers only library, and you only need to link -lboost_system -lboost_regex -lopenssl

undefined reference to boost::system::system_category() when compiling

I'm trying to compile a program on Ubuntu 11.10 that uses the Boost libraries. I have the 1.46-dev Boost libraries from the Ubuntu Repository installed, but I get an error when compiling the program.
undefined reference to boost::system::system_category()
What is it that I do wrong?
The boost library you are using depends on the boost_system library. (Not all of them do.)
Assuming you use gcc, try adding -lboost_system to your compiler command line in order to link against that library.
Linking with a library that defines the missing symbol (-lboost_system) is the obvious solution, but in the particular case of Boost.System, a misfeature in the original design makes it use boost::system::generic_category() and boost::system::system_category() needlessly. Compiling with the flag -DBOOST_SYSTEM_NO_DEPRECATED disables that code and lets a number of programs compile without requiring -lboost_system (that link is of course still needed if you explicitly use some of the library's features).
Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.
As noticed by #AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLY which enables a header-only version of the code. This was discouraged by Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.
Another workaround for those who don't need the entire shebang: use the switch
-DBOOST_ERROR_CODE_HEADER_ONLY.
If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).
The above error is a linker error... the linker a program that takes one or more objects generated by a compiler and combines them into a single executable program.
You must add -lboost_system to you linker flags which indicates to the linker that it must look for symbols like boost::system::system_category() in the library libboost_system.so.
If you have main.cpp, either:
g++ main.cpp -o main -lboost_system
OR
g++ -c -o main.o main.cpp
g++ main.o -lboost_system
When using CMAKE and find_package, make sure it is :
find_package(Boost COMPONENTS system ...)
and not
find_package(boost COMPONENTS system ...)
Some people may have lost hours for that ...
I got the same Problem:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-1_47 -lboost_filesystem-mgw45-mt-1_47
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54):
undefined reference to `boost::system::generic_category()
Solution was to use the debug-version of the system-lib:
g++ -mconsole -Wl,--export-all-symbols -LC:/Programme/CPP-Entwicklung/MinGW-4.5.2/lib -LD:/bfs_ENTW_deb/lib -static-libgcc -static-libstdc++ -LC:/Programme/CPP-Entwicklung/boost_1_47_0/stage/lib \
D:/bfs_ENTW_deb/obj/test/main_filesystem.obj \
-o D:/bfs_ENTW_deb/bin/filesystem.exe -lboost_system-mgw45-mt-d-1_47 -lboost_filesystem-mgw45-mt-1_47
But why?
When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_system last:
g++ mingw/timer1.o -o mingw/timer1.exe -L/usr/local/boost_1_61_0/stage/lib \
-lboost_timer-mgw53-mt-1_61 \
-lboost_chrono-mgw53-mt-1_61 \
-lboost_system-mgw53-mt-1_61
This was on mingw with gcc 5.3 and boost 1.61.0 with a simple timer example.
in my case, adding -lboost_system was not enough, it still could not find it in my custom build environment. I had to use the advice at Get rid of "gcc - /usr/bin/ld: warning lib not found" and change my ./configure command to:
./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME
for more details see Boost 1.51 : "error: could not link against boost_thread !"
...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:
<link>static
<library>/boost/system//boost_system
and perhaps also:
<linkflags>-static-libgcc
<linkflags>-static-libstdc++

Boost.Asio linking error

I'm trying access an extern device via a serial port and want to use Boost.Asio for this propose.
I have build the boost libraries for MinGw and compiled the regex example successful.
But I have problems to compile my code if I include something from Boost.Asio:
#include <boost/asio/serial_port.hpp>
int main() {
return 0;
}
g++ -D _WIN32_WINNT=0x0501 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\SerialPortTest.o ..\src\SerialPortTest.cpp
g++ -LC:\boost-libs\boost\bin.v2\libs\thread\build\gcc-mingw-4.5.2\release\link-static\threading-multi -LC:\boost-libs\boost\bin.v2\libs\system\build\gcc-mingw-4.5.2\release\link-static\threading-multi -oSerialPortTest.exe src\SerialPortTest.o -lboost_thread-mgw45-mt-1_48 -lboost_system-mgw45-mt-1_48
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `WSAStartup#8'
src\SerialPortTest.o: In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../include/boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to `WSACleanup#0'
collect2: ld returned 1 exit status
For me it seems to be a linking problem, but I don't get it.
Add -lws2_32 flag to link against WinSockets library.
Also, this might be useful: MinGW linker error: winsock
You miss wsock32 library. Add this to your dependencies and it should work.