c++ - 使用 Google 的正则表达式库 RE2 for C++ : linking errors

标签 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

关于我遗漏了什么有什么想法吗?

最佳答案

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

关于c++ - 使用 Google 的正则表达式库 RE2 for C++ : linking errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7605028/

相关文章:

c++ - 对泛型函数使用继承

c++ - Cocoa 会限制 C/C++ openGL 应用程序的整体性能吗?

C++:通过类或实例访问 const 成员变量?

c++ - 为什么这个初始化本身不会产生编译器警告?

c++ - 默认函数参数值在模板中可见但不应该 (gcc)

c++ - char16_t 和 char32_t 是 std 命名空间的一部分吗?

c++ - Qt错误信息 "qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed"

适用于 Windows 98 的 g++ 程序

使用正则表达式时 C++ 程序崩溃

c++ - 在 linux 下的 c++ 程序中包含 amp.h 库