c++ - 无法将 libpcrecpp 链接到 MinGW

标签 c++ linker mingw gnu pcre

编译/链接此代码时:

#include <string>
using std::string;

#include <pcrecpp.h>
using pcrecpp::RE;

int main() {
    string
        subj ("Hello world!"),
        rgx ("lolCat([0-9])"),
        result;
    RE(rgx).FullMatch(subj, &result);
}

... 通过使用此命令:

i586-mingw32msvc-g++ -std=c++11 -o test.exe -Ipcre-install/include test.cpp \
pcre-install/lib/libpcre.a \
pcre-install/lib/libpcrecpp.a \
pcre-install/lib/libpcreposix.a

...我收到此错误:

/tmp/ccAR72nT.o:test.cpp:(.text+0x119): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x123): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x12d): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x137): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x141): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x14b): more undefined references to `_imp___ZN7pcrecpp2RE6no_argE' follow
/tmp/ccAR72nT.o:test.cpp:(.text+0x1bd): undefined reference to `_imp___ZNK7pcrecpp2RE9FullMatchERKNS_11StringPieceERKNS_3ArgES6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_'
/tmp/ccAR72nT.o:test.cpp:(.text+0x1d6): undefined reference to `_imp___ZN7pcrecpp2RED1Ev'
/tmp/ccAR72nT.o:test.cpp:(.text+0x294): undefined reference to `_imp___ZN7pcrecpp2RED1Ev'
/tmp/ccAR72nT.o:test.cpp:(.text$_ZN7pcrecpp3ArgC1EPSs[__ZN7pcrecpp3ArgC1EPSs]+0x16): undefined reference to `_imp___ZN7pcrecpp3Arg12parse_stringEPKciPv'
/tmp/ccAR72nT.o:test.cpp:(.text$_ZN7pcrecpp2REC1ERKSs[__ZN7pcrecpp2REC1ERKSs]+0x6a): undefined reference to `_imp___ZN7pcrecpp2RE4InitERKSsPKNS_10RE_OptionsE'
collect2: error: ld returned 1 exit status

我做错了什么吗?

libpcrecpp 使用此配置编译:

../pcre3-8.35/configure --host=i586-mingw32msvc \
--enable-shared=no --enable-static=yes \
--prefix="$(readlink -m ../pcre-install)"

最佳答案

您已将pcrecpp 编译为静态库,您需要在编译代码时定义PCRE_STATIC,请参阅https://github.com/vmg/pcre/blob/a257f5c7acc12e64dc2b5aa170b8e4b87dc34f83/pcreposix.h#L117

i586-mingw32msvc-g++ -std=c++11 -o test.exe -DPCRE_STATIC -Ipcre-install/include test.cpp \
pcre-install/lib/libpcre.a \
pcre-install/lib/libpcrecpp.a \
pcre-install/lib/libpcreposix.a

如果没有 PCRE_STATIC,所有公共(public)函数都标记为 dllimport并且有不同的名称修改

关于c++ - 无法将 libpcrecpp 链接到 MinGW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765515/

相关文章:

c++ - 使用 MinGW 时 NetBeans Make 命令

c++ - 比较不同类型的 C++ for 循环

c++ - 正在进行的返回值转换的说明

c++ - C++中没有模板的模板类的decltype

gcc - Ubuntu 升级后链接到库的问题

c# - 我在说 "Assertion failed"之前从未见过的奇怪的 xCode 链接器错误

windows - 当我想使用 MinGW 时,系统 PATH 中的 ndk bin 出现问题

c++ - 释放分配给存储在 vector 中的堆的内存

c++ - gcc 链接器 : which input libraries are used?

c - MinGW 控制台应用程序