【问题标题】:Undefined reference when Gcc compiling Boost::regex with multiple versions of boost exist当 Gcc 使用多个版本的 boost 编译 Boost::regex 时存在未定义的引用
【发布时间】:2012-05-03 21:35:02
【问题描述】:

我试图在 linux 机器上安装 Boost(CentOs,Linux 版本 2.6.9-67.ELsmp)。 我按照 Boost Getting Started 网页上的说明进行操作。 下载并解压文件,然后我添加前缀,因为我不是root用户。

./bootstrap.sh --prefix=/my_path/boost-1.49.0
./b2 install

我去 /my_path/boost-1.49.0 并检查那里有 /include/boost/ 和 /lib/ 包含文件。

我还将 LD_LIBRARY_PATH 设置为 /my_path/boost-1.49.0/lib/。

为了测试我是否安装成功,我编译了以下代码:

main.cpp

#include <boost/regex.hpp>
#include <iostream>
#include <string>

using namespace boost;
using namespace std;

int main(int argc, char *argv[])
{
    string line = "12345";
    regex pattern("^123");
    if (regex_match(line, pattern)) cout << "match." << endl;
    else cout << "not match." << endl;
    return 1;
}

并通过命令编译:

g++ -v -I /my_path/boost-1.49.0/include/boost -L /my_path/boost-1.49.0/lib main.cpp -lboost_regex -o example

并产生以下错误:

Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
/usr/libexec/gcc/x86_64-redhat-linux/3.4.6/cc1plus -quiet -v -I /my_path/boost-1.49.0/include/boost/ -D_GNU_SOURCE main.cpp -quiet -dumpbase main.cpp -mtune=k8 -auxbase main -version -o /tmp/ccW0ON8y.s
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/my_path/boost-1.49.0/include/boost/
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/backward
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/include
/usr/include
End of search list.
GNU C++ version 3.4.6 20060404 (Red Hat 3.4.6-9) (x86_64-redhat-linux)
        compiled by GNU C version 3.4.6 20060404 (Red Hat 3.4.6-9).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
as -V -Qy -o /tmp/ccpu82sB.o /tmp/ccW0ON8y.s
GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version 2.15.92.0.2 20040927
/usr/libexec/gcc/x86_64-redhat-linux/3.4.6/collect2 --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o example /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/3.4.6/crtbegin.o -L /my_path/boost-1.49.0/lib/ -L/usr/lib/gcc/x86_64-redhat-linux/3.4.6 -L/usr/lib/gcc/x86_64-redhat-linux/3.4.6 -L/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../.. -L/lib/../lib64 -L/usr/lib/../lib64 /tmp/ccpu82sB.o -lboost_regex -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/3.4.6/crtend.o /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crtn.o
/tmp/ccpu82sB.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_
traitsIcEESaIcEEC1EPKcjRKS3_+0x27): In function `boost::basic_regex<char, 
boost::regex_traits<char>, std::allocator<char> >::basic_regex(char const*, 
unsigned int, std::allocator<char> const&)':: 
undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, 
std::allocator<char> >::reg_expression(char const*, unsigned int, 
std::allocator<char> const&)'

/tmp/ccpu82sB.o(.gnu.linkonce.t.
_ZN5boost11basic_regexIcNS_12regex_traitsIcEESaIcEED1Ev+0x11): In function 
`boost::basic_regex<char, boost::regex_traits<char>, 
std::allocator<char>>::~basic_regex()':
: undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, 
std::allocator<char> >::~reg_expression()'
/tmp/ccpu82sB.o(.gnu.linkonce.t.
_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub
_matchIS6_EEENS_12regex_traitsIcEESaIcEEC1ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_
14reg_expressionIcSB_SC_EENS_15regex_constants12_match_flagsE+0xa1): In function 
`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>, std::allocator<char>>
::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char 
const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
boost::match_results<__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::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&, 
boost::regex_constants::_match_flags)':

但是,如果我只是使用系统管理员安装的默认 boost 进行编译

g++ -v main.cpp -lboost_regex -o example

那就好了。

我得到的ldd示例

libboost_regex.so.1 => /usr/lib64/libboost_regex.so.1 (0x0000002a95557000)

由于某种原因,我被要求不要更新 /usr/include/boost 中的 boost。 有什么可以帮助我解决这个问题的方法,即在本地文件夹中安装 boost 并更改路径如何包含和链接以使用不同的版本?

我不确定这个问题是否愚蠢,但我确实用谷歌搜索过,但主要是 相关问题未在 gcc 命令中正确设置 -I、-L 和 -l。我不知道 上面的错误信息,真的把我逼疯了。任何帮助表示赞赏。

感谢您的耐心和时间。

【问题讨论】:

  • 链接器显然不包括您安装的 boost 库。将-L &lt;your lib path&gt; 更改为-L&lt;your lib path&gt; 是否有效?
  • 不。事实上,我的编译命令在 -L 和路径之间没有空格。发帖打错了,不好意思。

标签: c++ linux gcc boost boost-regex


【解决方案1】:

可能是因为在您的编译命令中,您说使用/my_path/boost-1.49.0/include/boost 作为包含路径,然后在您的源代码中使用#include &lt;boost/regex.hpp&gt;

我的猜测是regex.hpp/my_path/boost-1.49.0/include/boost/boost/regex.hpp 中搜索。显然没有找到,所以找到了系统一。

你可以试试: g++ -v -I /my_path/boost-1.49.0/include -L /my_path/boost-1.49.0/lib main.cpp -lboost_regex -o example

【讨论】:

  • 哦,谢谢!!这正是问题的原因,没有注意到那个微妙的部分。
猜你喜欢
  • 2015-08-23
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多