无法链接libpcrecpp w / MinGW

时间:2015-01-04 12:30:31

标签: 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)"

2 个答案:

答案 0 :(得分:2)

您已将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所有标记为dllimport的公共函数,并且具有不同的名称修改

答案 1 :(得分:1)

您必须定义PCRE_STATIC(即-DPCRE_STATIC)。