C++ Boost: undefined reference to boost::system::generic_category()
You should link in the libboost_system
library. I am not sure about codeblocks, but the g++ command-line option on your platform would be
-lboost_system
undefined reference to boost::system::system_category() when compiling
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.
Undefined reference to boost::system::generic_category() although libs are given to g++
Your build command is in the wrong order, and order matters.
GCC reads left-to-right, taking symbols from libraries when it already knows it needs them. As you put program.cpp
last, you don't make that known until all listed libraries have already been identified and discarded.
Put program.cpp
first, then the libraries it needs.
g++ -o program program.cpp -lboost_filesystem -ldl -lboost_system
Yes, it's kind of weird. (Even weirder that it worked on Debian! Though apparently only some "recent" Linuxy distributions default --as-needed
on, which is what causes the behaviour you see, showing that the behaviour isn't necessarily guaranteed. Perhaps Debian 9 just outright does not do that.)
More info:
- Why does the order in which libraries are linked sometimes cause errors in GCC?
why undefined reference to `boost::system::generic_category even if I do link against boost_system
The order at which you link your libraries matters, in your case you have library.cpp
that apparently uses the boost_system
library
library.cpp:(.text+0x25f): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x269): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x273): undefined reference to `boost::system::system_category()'
To solve this you should move the boost_system library to the end of your link line
g++ -o build/myproject build/main/main.o -L/usr/local/boost/boost_1_52_0/boost/libs -L/usr/lib -Lbuild -L. -lboost_thread -lpthread -lboost_regex -lpq -lmylibrary **-lboost_system**
Alternatively, build libmylibrary.so
as a shared library and link to the boost_system
library directly.
undefined reference to `boost::system::generic_category()' when adding boost/asio
The -l
option is not a compiler option, it's a linker options, so you're setting it for the wrong variable as CMAKE_CXX_FLAGS
are only for the compiler.
Instead use e.g. target_link_libraries
to add libraries. Like
target_link_libraries(rcp boost_system)
But what you really should do is to find the system-installed Boost libraries and use those. You do that with find_package
:
find_package(Boost
REQUIRED COMPONENTS asio system)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(rcp ${Boost_LIBRARIES})
Related Topics
How to Avoid the Diamond of Death When Using Multiple Inheritance
Find Out Whether a C++ Object Is Callable
C++ Array Assignment of Multiple Values
Shared-Memory Ipc Synchronization (Lock-Free)
How to Sort an Stl Map by Value
C and C++ Programming on Ubuntu 11.10
How to Get Memory Usage Under Windows in C++
What Is the Easiest Way to Print a Variadic Parameter Pack Using Std::Ostream
Why Is It Allowed to Pass R-Values by Const Reference But Not by Normal Reference
How to Make C++ Cout Not Use Scientific Notation
Gcc C++ "Hello World" Program -> .Exe Is 500Kb Big When Compiled on Windows. How to Reduce Its Size
How to Define a Template Function Within a Template Class Outside of the Class Definition
Sendinput() Not Equal to Pressing Key Manually on Keyboard in C++
Convert Std::Bind to Function Pointer