103

I am trying to include Boost libraries in my project and have been facing issues in the same. I am on Ubuntu 12.10 with Codeblocks IDE and tried installing the libraries manually reading instructions from the site, but was getting error's with header as well as to-be-built-before-use libraries.

I then installed the libraries via terminalby sudo apt-get install libboost-all-dev. After this, in my programs on Codeblocks, I can include headers like #include <boost/regex.hpp> but when I try to include the header for the Filesystem library ( #include "boost/filesystem/operations.hpp" ), I am getting the following error:

/usr/include/boost/system/error_code.hpp|214|undefined reference to boost::system::generic_category()'|

I am not sure how to resolve this error (specifically in Codeblocks on Linux). I really could use some help here.

Compiler : Gcc
Program code: Only tried inlcuding the above file system operations.hpp file.

Build log from Codeblocks:

Build started on: 20-11-2012 at 18:02.53
Build ended on: 20-11-2012 at 18:02.54
-------------- Build: Debug in libopenFrameworks ---------------
Target is up to date.
-------------- Build: Debug in reader1 ---------------
make -s -f Makefile Debug
linking i686 bin/reader1_debug linux
obj/i686Debug/src/testApp.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
obj/i686Debug/src/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [bin/reader1_debug] Error 1
Process terminated with status 2 (0 minutes, 1 seconds)
6 errors, 0 warnings
Cipher
  • 5,894
  • 22
  • 76
  • 112
  • your not linking it right, codeblocks won't do it for you unlike you system path and system repository, – pyCthon Nov 20 '12 at 05:28
  • @pyCthon: In that case, can you add more detail more detail about how to link this in Code::blocks. I tried adding the linker flag `-lboost_system` at some place in settings, but it didn't resolve. However, I am not even sure if I did the whole linker flag thing at the right place. A little more detail would be of great help. – Cipher Nov 20 '12 at 05:33
  • well what compiler are you using? mingw? gcc? what code are you trying to run? give an example..... – pyCthon Nov 20 '12 at 05:47
  • @pyCthon: Comiler is Gcc. I have not written any code till now, but have tried including the `Boost filesystem library` (operation.hpp in the above post) – Cipher Nov 20 '12 at 05:52
  • does it work compiling a regular program with boost filesystem library from command line with g++? – pyCthon Nov 20 '12 at 15:26

13 Answers13

154

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

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I already found the linker flag to be added though. Can't get it working in Codeblocks. – Cipher Nov 20 '12 at 09:36
  • @Cipher can you obtain the build and link commands executed by codeblocks? – juanchopanza Nov 20 '12 at 12:12
  • @Cipher sorry, pastebin is blocked at work. I can check later but it will take a few hours. – juanchopanza Nov 20 '12 at 12:39
  • No problem. Updated in the post as well – Cipher Nov 20 '12 at 15:21
  • i have the same problem as Cipher's, just in my case i deal with boost/asio.hpp. I get these error http://s1.directupload.net/images/130608/koyvqpdp.jpg when trying to compiler. When i try to add -lboost_system it says "cannot find lboost_system". What;s the problem? – ddacot Jun 08 '13 at 12:39
  • @ddacot are the boost libs in your library search path? – juanchopanza Jun 08 '13 at 12:43
  • @juanchopanza i'm not quiet sure what are you talking about, but i have these lines in my .pro file INCLUDEPATH += C:/boost/boost_1_53_0/ LIBS += "-LC:/boost/boost_1_53_0/stage/lib/" -lboost_system – ddacot Jun 08 '13 at 13:10
  • @ddacot OK, you seem to be on some windows system, probably using mingw, right? I am not familiar with the exact options you need for that. – juanchopanza Jun 08 '13 at 13:15
  • @juanchopanza some days ago i compiled boost for vs2012 and it works fine. Now i need to use in qt but can't. Yeap, i'm on w8. Yes Qt creator uses Mingw as default compiler. – ddacot Jun 08 '13 at 13:18
  • In cmake this will be include_directories(${libfoo_INCLUDE_DIRS}) and then target_link_library(MyLib ${libfoo_LDFLAGS}). – Jonas G. Drange Dec 13 '15 at 19:16
  • if you are using CMake the following lines should be added: add_library(boost_system STATIC IMPORTED) set_target_properties(boost_system PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/libs/${ANDROID_ABI}/libboost_system-gcc-mt-1_53.a") add_library(boost_filesystem STATIC IMPORTED) set_target_properties(boost_filesystem PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/libs/${ANDROID_ABI}/libboost_filesystem-gcc-mt-1_53.a") – Ashraf Sayied-Ahmad Oct 16 '17 at 12:06
21

Depending on the boost version libboost-system comes with the -mt suffix which should indicate the libraries multithreading capability.

So if -lboost_system cannot be found by the linker try -lboost_system-mt.

tssch
  • 734
  • 8
  • 25
  • 1
    It works because for these boost versions the actual library file name had a `-mt` suffix. – tssch Feb 21 '19 at 09:22
11

This answer actually helped when using Boost and cmake.

Adding add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY) for cmake file.

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.12)
project(proj)

set(CMAKE_CXX_STANDARD 17)


set(SHARED_DIR "${CMAKE_SOURCE_DIR}/../shared")

set(BOOST_LATEST_DIR            "${SHARED_DIR}/boost_1_68_0")
set(BOOST_LATEST_BIN_DIR        "${BOOST_LATEST_DIR}/stage/lib")
set(BOOST_LATEST_INCLUDE_DIR    "${BOOST_LATEST_DIR}/boost")
set(BOOST_SYSTEM                "${BOOST_LATEST_BIN_DIR}/libboost_system.so")
set(BOOST_FS                    "${BOOST_LATEST_BIN_DIR}/libboost_filesystem.so")
set(BOOST_THREAD                "${BOOST_LATEST_BIN_DIR}/libboost_thread.so")

set(HYRISE_SQL_PARSER_DIR           "${SHARED_DIR}/hyrise_sql_parser")
set(HYRISE_SQL_PARSER_BIN_DIR       "${HYRISE_SQL_PARSER_DIR}")
set(HYRISE_SQL_PARSER_INCLUDE_DIR   "${HYRISE_SQL_PARSER_DIR}/src")
set(HYRISE_SQLPARSER                "${HYRISE_SQL_PARSER_BIN_DIR}/libsqlparser.so")


include_directories(${CMAKE_SOURCE_DIR} ${BOOST_LATEST_INCLUDE_DIR} ${HYRISE_SQL_PARSER_INCLUDE_DIR})

set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu/")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)

find_package(Boost 1.68.0 REQUIRED COMPONENTS system thread filesystem)

add_executable(proj main.cpp row/row.cpp row/row.h table/table.cpp table/table.h page/page.cpp page/page.h
        processor/processor.cpp processor/processor.h engine_instance.cpp engine_instance.h utils.h
        meta_command.h terminal/terminal.cpp terminal/terminal.h)


if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(proj PUBLIC Boost::system Boost::filesystem Boost::thread ${HYRISE_SQLPARSER})
endif()
sdd
  • 721
  • 9
  • 23
9

It's a linker problem. Include the static library path into your project.

For Qt Creator open the project file .pro and add the following line:

LIBS += -L<path for boost libraries in the system> -lboost_system

In my case Ubuntu x86_64:

LIBS += -L/usr/lib/x86_64-linux-gnu -lboost_system

For Codeblocks, open up Settings->Compiler...->Linker settings tab and add:

boost_system

to the Link libraries text widget and press OK button.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
GLR
  • 157
  • 2
  • 7
8

I searched for a solution as well, and none of the answers I encountered solved the error, Until I found the answer of "ViRuSTriNiTy" to this thread: Undefined reference to 'boost::system::generic_category()'?

according to that answer, try to add these lines to your cmake file:

find_package(Boost 1.55.0 REQUIRED COMPONENTS system filesystem)
include_directories(... ${Boost_INCLUDE_DIRS})
link_directories(... ${Boost_LIBRARY_DIRS})
target_link_libraries(... ${Boost_LIBRARIES})
Community
  • 1
  • 1
SubMachine
  • 487
  • 3
  • 11
4

Same problem on building a simple boost example, solved after i changed the g++ compiler flag from -std=c++14 to -std=c++11.

And I noticed that it's a C++11 Example...

Halowb
  • 77
  • 2
3

I had the same problem and also use Linux Mint (as nuduoz) . I my case problem was solved after i added boost_system to GCC C++ Linker->Libraries.

piotr93
  • 41
  • 3
1

You could come across another problem. After installing Boost on the Linux Mint I've had the same problem. Linking -lboost_system or -lboost_system-mt haven't worked because library have had name libboost_system.so.1.54.0.

So the solution is to create symbolic link to the original file. In my case

sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 /usr/lib/libboost_system.so

For more information see this question.

Community
  • 1
  • 1
nuduoz
  • 57
  • 2
  • 10
1

g++ -lboost_system -lboost_filesystem userentry.cpp -o userentry

worked perfectly under debian. (boost c++ libraries installed with apt-get).

majukarma
  • 11
  • 1
1

Il the library is not installed you should give boost libraries folder:

example:

g++ -L/usr/lib/x86_64-linux-gnu -lboost_system -lboost_filesystem prog.cpp -o prog

0

try

g++ -c main.cpp && g++ main.o /usr/lib/x86_64-linux-gnu/libboost_system.so && ./a.out 

/usr/lib/x86_64-linux-gnu/ is the location of the boost library

use find /usr/ -name '*boost*.so' to find the boost library location

algometrix
  • 3,704
  • 2
  • 14
  • 22
0

After testing the proposed solutions described above, I found only these few of lines would work.

I am using Ubuntu 16.04.

cmake_minimum_required(VERSION 3.13)  
project(myProject)  

set(CMAKE_CXX_STANDARD 11)  
add_executable(myProject main.cpp)  

find_package(Boost 1.58.0 REQUIRED COMPONENTS system filesystem)  
target_link_libraries(myProject ${Boost_LIBRARIES})
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Alan Yang
  • 66
  • 5
0

In my project I prefer to use header-only libraries. So nothing above was helpful. What did really help is:

add_definitions(-DBOOST_SYSTEM_NO_DEPRECATED)
def
  • 521
  • 4
  • 16