链接以增强gcc中的正则表达式

时间:2009-02-17 23:21:09

标签: c++ regex boost linker boost-regex

我正在尝试编译在linux上使用正则表达式的程序。我在中创建了boost库     林达/正则表达式/编译 通过打字     make -fgcc.mak 它创建了一个目录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

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

我收到的错误表明找不到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)'

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

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:0)

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

摘自My Original(有问题)Makefile: LIBS = -static -lboost_filesystem

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

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

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