C + + Boost: 对 Boost: : system: : general_Category()的未定义引用

我正在尝试将 Boost库包含在我的项目中,并且一直面临着同样的问题。我在 Ubuntu 12.10上使用了 Codeblock IDE,并尝试从站点手动安装这些库来阅读说明,但是在头部以及使用前需要构建的库方面出现了错误。

然后,我通过 sudo apt-get install libboost-all-dev终端安装了这些库。在这之后,在我的 Codeblock 程序中,我可以包含像 #include <boost/regex.hpp>这样的头文件,但是当我试图包含文件系统库(#include "boost/filesystem/operations.hpp")的头文件时,我得到了以下错误:

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

我不确定如何解决这个错误(特别是在 Linux 上的 Codeblock 中)。我真的需要帮助。

编译器: Gcc
程序代码: 只尝试包含上述文件系统 operations.hpp文件。

从代码块构建日志:

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
136429 次浏览

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

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.

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.

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.

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

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.

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})

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...

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

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

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

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()

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})

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)