在编译时未定义的对 ost: : system: : system_Category()的引用

我试图在 Ubuntu 11.10上编译一个使用 Boost 库的程序。我已经安装了 Ubuntu Repository 中的1.46-dev Boost 库,但是在编译程序时遇到了一个错误。

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

What is it that I do wrong?

159831 次浏览

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.

上面的错误是一个链接器错误... 链接器 将编译器生成的一个或多个对象组合成一个可执行程序的程序。

您必须将 -lboost_system添加到链接器标志中,这表明链接器必须在库 libboost_system.so中查找类似 boost::system::system_category()的符号。

如果使用 main.cpp,可以选择:

g++ main.cpp -o main -lboost_system

或者

g++ -c -o main.o main.cpp
g++ main.o -lboost_system

... 如果你想静态链接你的主文件,在你的 Jamfile 中添加以下要求:

<link>static
<library>/boost/system//boost_system

也许还有:

<linkflags>-static-libgcc
<linkflags>-static-libstdc++

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): 未定义的对‘ Boost: : system: : general _ 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

但是为什么呢?

在我的例子中,添加 -lboost_system是不够的,它仍然无法在我的自定义构建环境中找到它。我不得不使用 删除“ gcc-/usr/bin/ld: 未找到警告库”的建议,并将我的 ./configure命令更改为:

./configure CXXFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib -Wl,-rpath-link,$HOME/lib" --with-boost-libdir=$HOME/lib --prefix=$HOME

详情请参阅 Boost 1.51: “ error: could not link against ost _ thread!”

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

从 Boost 1.66和 这个承诺开始,这个行为现在是默认的,所以希望越来越少的用户需要这个答案。

正如@AndrewMarshall 所注意到的,另一种方法是定义 BOOST_ERROR_CODE_HEADER_ONLY,它支持代码的标题版本。这是由 Boost 的 气馁,因为它可以破坏一些功能。然而,自从1.69以来,头文件似乎只有 成为默认的,据说这个问题已经过时了。

在使用 CMAKE 和 find _ package 时,请确保它是:

find_package(Boost COMPONENTS system ...)

而不是

find_package(boost COMPONENTS system ...)

有些人可能因此浪费了好几个小时。

当我遇到这个问题时,原因是库的排序。为了解决这个问题,我把 libboost_system放在最后:

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.

对于那些不需要全套服务的人来说,另一个解决办法是: 使用开关

-DBOOST_ERROR_CODE_HEADER_ONLY.

If you use CMake, it's add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).