C++ 链接到 gcc 中的 boost regex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/559179/
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 15:57:21  来源:igfitidea点击:

linking to boost regex in gcc

c++regexboostlinkerboost-regex

提问by greyfade

i am trying to compile my program which uses regex on linux. I built the boost library in the libs/regex/build by typing make -fgcc.mak which created a directory gcc which contains the following four files

我正在尝试编译我的程序,它在 linux 上使用正则表达式。我通过键入 make -fgcc.mak 在 libs/regex/build 中构建了 boost 库,它创建了一个包含以下四个文件的目录 gcc

boost_regex-gcc-1_35
boost_regex-gcc-d-1_35
libboost_regex-gcc-1_35.a
libboost_regex-gcc-d-1_35.a

Now I want to use regex from my program which is in some arbitrary directory. I #included boost/regex.hpp

现在我想使用我的程序中的正则表达式,它位于某个任意目录中。我#included boost/regex.hpp

I got the error which stated that regex.hpp is not found. Then I gave the -I option in the g++ compiler. I didn't get that error. But I get the following error

我收到错误,指出未找到 regex.hpp。然后我在 g++ 编译器中给了 -I 选项。我没有得到那个错误。但我收到以下错误

undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'

I googled and found that I need to somehow link one of the above 4 libraries to my program. How can I do it. Which one should I link and why?

我用谷歌搜索,发现我需要以某种方式将上述 4 个库之一链接到我的程序。我该怎么做。我应该链接哪一个,为什么?

回答by greyfade

Either add libboost_regex-gcc-1_35.ato your list of object files in your link step or add -static -lboost_regex-gcc-1_35to the same. Also be sure that you have an -Iswitch pointing to your boost includes directory in your compile step. If the libraries are outside the typical search path (/usr/libon *nix), add that directory to your link command with -Wl,-L/path/to/boost/libsfor g++or simply -L/path/to/boost/libson ld.

libboost_regex-gcc-1_35.a在链接步骤中添加到您的目标文件列表或添加-static -lboost_regex-gcc-1_35到相同的。还要确保-I在编译步骤中有一个指向 boost 包含目录的开关。如果库是典型的搜索路径(外面/usr/lib在* nix),该目录添加到您的链接命令-Wl,-L/path/to/boost/libs用于g++或者干脆-L/path/to/boost/libsld

回答by sonofdelphi

I also faced similar problems when using boost filesystem. Here's what I needed to do to get it to link statically.

我在使用 boost 文件系统时也遇到了类似的问题。这是我需要做的才能让它静态链接。

Excerpt from My Original (problematic) Makefile: LIBS = -static -lboost_filesystem

摘自我的原始(有问题)Makefile:LIBS = -static -lboost_filesystem

Solution: LIBS = -Wl,-Bstatic -lboost_filesystem -lboost_system -Wl,-Bdynamic

解决方案:LIBS = -Wl,-Bstatic -lboost_filesystem -lboost_system -Wl,-Bdynamic

You can view the complete Makefile from http://code.google.com/p/neptuner/source/browse/codebase/trunk/stratego/uboat/Makefile

您可以从http://code.google.com/p/neptuner/source/browse/codebase/trunk/stratego/uboat/Makefile查看完整的 Makefile

Needed to add boost_system to make it link properly. Direct addition/specification of libboost*.a created more problems. Note the -Bdynamic is present to prevent static link of standard libraries.

需要添加 boost_system 以使其正确链接。直接添加/指定 libboost*.a 会产生更多问题。请注意 -Bdynamic 的存在是为了防止标准库的静态链接。