使用Google的正则表达式库RE2 for C ++:链接错误

时间:2011-09-30 01:13:52

标签: c++ g++

我正在尝试在我的C ++代码中使用Google的正则表达式库RE2,但无法将其编译。这是我写的一个简单的测试程序:

#include <iostream>
#include <re2/re2.h>

using namespace std;
using namespace re2;

int main (int argc, char **argv)
{
    cout << "hello world" << endl;

    int matchResult;

    matchResult = RE2::FullMatch("hello", "h.*o");
    cout << "matchResult = " << matchResult << endl;

    return 0;
}

当我尝试使用这个g ++命令编译它时:

g++ -I /usr/local/include -o test main.cc

我收到这些错误:

/var/tmp//ccOMm5QM.o(.text+0x1f2): In function `main':
: undefined reference to `re2::RE2::RE2(char const*)'
/var/tmp//ccOMm5QM.o(.text+0x210): In function `main':
: undefined reference to `re2::RE2::FullMatch'
/var/tmp//ccOMm5QM.o(.text+0x227): In function `main':
: undefined reference to `re2::RE2::~RE2()'
/var/tmp//ccOMm5QM.o(.text+0x275): In function `main':
: undefined reference to `re2::RE2::~RE2()'
/var/tmp//ccOMm5QM.o(.gnu.linkonce.t._ZNK3re217VariadicFunction2IbRKNS_11StringPieceERKNS_3RE2ENS4_3ArgEXadL_ZNS4_10FullMatchNES3_S6_PKPKS7_iEEEclES3_S6_+0x27): In function `re2::VariadicFunction2<bool, re2::StringPiece const&, re2::RE2 const&, re2::RE2::Arg, &(re2::RE2::FullMatchN(re2::StringPiece const&, re2::RE2 const&, re2::RE2::Arg const* const*, int))>::operator()(re2::StringPiece const&, re2::RE2 const&) const':
: undefined reference to `re2::RE2::FullMatchN(re2::StringPiece const&, re2::RE2 const&, re2::RE2::Arg const* const*, int)'

我尝试使用“-L”选项:

g++ -I /usr/local/include -L/usr/local/lib -libre2 -o test main.cc

但是得到这个错误:

/usr/bin/ld: cannot find -libre2.so

即使图书馆存在:

$ ls -l /usr/local/lib/libre2*
-rwxr-xr-x  1 root  root  9322236 Sep 28 12:00 /usr/local/lib/libre2.a
lrwxr-xr-x  1 root  root       15 Sep 28 12:00 /usr/local/lib/libre2.so -> libre2.so.0.0.0
lrwxr-xr-x  1 root  root       15 Sep 28 12:00 /usr/local/lib/libre2.so.0 -> libre2.so.0.0.0
-rwxr-xr-x  1 root  root  3597784 Sep 28 12:00 /usr/local/lib/libre2.so.0.0.0

关于我缺少的任何想法?

2 个答案:

答案 0 :(得分:2)

-l选项表示添加要链接的库。所以,当你说-libre2时,它意味着“链接到库ibre2”,这可能不是你的意思。使用-lre2

答案 1 :(得分:0)

更新LD_LIBRARY_PATH以包含re2库路径