C++ 编译时对 boost::system::system_category() 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9723793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:14:01  来源:igfitidea点击:

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

c++boost

提问by user1049697

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.

我正在尝试在 Ubuntu 11.10 上编译一个使用 Boost 库的程序。我安装了 Ubuntu 存储库中的 1.46-dev Boost 库,但在编译程序时出现错误。

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

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

What is it that I do wrong?

我做错了什么?

回答by hc_

The boost library you are using depends on the boost_system library. (Not all of them do.)

您使用的 boost 库取决于 boost_system 库。(并非所有人都这样做。)

Assuming you use gcc, try adding -lboost_systemto your compiler command line in order to link against that library.

假设您使用 gcc,请尝试添加-lboost_system到您的编译器命令行以链接到该库。

回答by Marc Glisse

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

与定义的缺失的符号(一个库链接-lboost_system)是明显的解决方案,但在Boost.System的特定情况下,在原始设计中的不好的特性使得它使用boost::system::generic_category()boost::system::system_category()不必要。使用该标志进行编译-DBOOST_SYSTEM_NO_DEPRECATED会禁用该代码,并允许许多程序在不需要的情况下进行编译-lboost_system(如果您明确使用该库的某些功能,当然仍然需要该链接)。

Starting from Boost 1.66 and this commit, this behavior is now the default, so hopefully fewer and fewer users should need this answer.

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

As noticed by @AndrewMarshall, an alternative is to define BOOST_ERROR_CODE_HEADER_ONLYwhich enables a header-only version of the code. This was discouragedby Boost as it can break some functionality. However, since 1.69, header-only seems to have become the default, supposedly making this question obsolete.

正如@AndrewMarshall 所注意到的,另一种方法是定义BOOST_ERROR_CODE_HEADER_ONLY启用仅标头版本的代码。Boost不鼓励这样做,因为它会破坏某些功能。但是,从 1.69 开始, header-only 似乎已成为默认值,据说这个问题已经过时了。

回答by Vadim Berman

Another workaround for those who don't need the entire shebang: use the switch

对于不需要整个shebang的人的另一种解决方法:使用开关

-DBOOST_ERROR_CODE_HEADER_ONLY.

-DBOOST_ERROR_CODE_HEADER_ONLY.

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

如果你使用 CMake,它是add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY).

回答by user1055604

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

您必须-lboost_system向链接器添加标志,该标志向链接器指示它必须查找类似boost::system::system_category()库中的符号libboost_system.so

If you have main.cpp, either:

如果你有 main.cpp,要么:

g++ main.cpp -o main -lboost_system

OR

或者

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

回答by Kriegalex

When using CMAKE and find_package, make sure it is :

使用 CMAKE 和 find_package 时,请确保它是:

find_package(Boost COMPONENTS system ...)

and not

并不是

find_package(boost COMPONENTS system ...)

Some people may have lost hours for that ...

有些人可能为此失去了几个小时......

回答by volker

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

D:/bfs_ENTW_deb/obj/test/main_filesystem.obj:main_filesystem.cpp:(.text+0x54): 对 `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?

但为什么?

回答by Min Zhang

When I had this, problem, the cause was the ordering of the libraries. To fix it, I put libboost_systemlast:

当我遇到这个问题时,原因是库的排序。为了解决它,我把它放在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.

这是在 mingw 上使用 gcc 5.3 和 boost 1.61.0 使用一个简单的计时器示例。

回答by jcomeau_ictx

in my case, adding -lboost_systemwas 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 ./configurecommand to:

就我而言,添加-lboost_system还不够,它仍然无法在我的自定义构建环境中找到它。我不得不使用摆脱“gcc - /usr/bin/ld:warning lib not found”中的建议并将我的./configure命令更改为:

./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 !"

有关更多详细信息,请参阅Boost 1.51:“错误:无法链接 boost_thread !”

回答by formiaczek

...and in case you wanted to link your main statically, in your Jamfile add the following to requirements:

...如果您想静态链接您的主要内容,请在您的 Jamfile 中将以下内容添加到要求中:

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

and perhaps also:

也许还有:

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