you also need to link against "pcre.lib". If you have a look at the "pcrecpp.h" source code you will see that it includes "pcre.h" so it also uses the "c part" of the library.
MS VC++ 9 pcrecpp.lib linking error
-
07-10-2022 - |
Question
Just built pcre-8.34
for MS VC++ under Windows x86, copied the libs (pcrecpp.lib) and the headers (pcrecpp.h, pcrecpparg.h, pcre_stringpiece.h) to their locations and wanted to test a simple code (that works under GNU Linux C++) but I'm getting some linking errors:
#define PCRE_STATIC 1
#include <pcrecpp.h>
#include <iostream>
#pragma comment(lib,"pcrecpp.lib")
using namespace std;
int main(void)
{
pcrecpp::RE regex("(hello)");
std::string strBase = "hello hello hello";
pcrecpp::StringPiece input(strBase);
std::string match;
int count = 0;
while (regex.FindAndConsume(&input, &match)) {
count++;
std::cout << count << " " << match << std::endl;
}
}
Building output:
C:\Documents and Settings\Administrator\Desktop\Coding\pcre>cl.exe /MD /EHsc /O2 pc.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
pc.cpp Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved.
/out:pc.exe pc.obj pcrecpp.lib(pcrecpp.obj) : error LNK2019: unresolved external symbol _pcre_compi le referenced in function "private: struct real_pcre * __thiscall pcrecpp::RE::C ompile(enum pcrecpp::RE::Anchor)" (?Compile@RE@pcrecpp@@AAEPAUreal_pcre@@W4Ancho r@12@@Z) pcrecpp.lib(pcrecpp.obj) : error LNK2019: unresolved external symbol _pcre_confi g referenced in function "int __cdecl pcrecpp::NewlineMode(int)" (?NewlineMode@p crecpp@@YAHH@Z) pcrecpp.lib(pcrecpp.obj) : error LNK2019: unresolved external symbol _pcre_exec referenced in function "private: int __thiscall pcrecpp::RE::TryMatch(class pcre cpp::StringPiece const &,int,enum pcrecpp::RE::Anchor,bool,int *,int)const " (?T ryMatch@RE@pcrecpp@@ABEHABVStringPiece@2@HW4Anchor@12@_NPAHH@Z) pcrecpp.lib(pcrecpp.obj) : error LNK2019: unresolved external symbol _pcre_fulli nfo referenced in function "public: int __thiscall pcrecpp::RE::NumberOfCapturin gGroups(void)const " (?NumberOfCapturingGroups@RE@pcrecpp@@QBEHXZ) pcrecpp.lib(pcrecpp.obj) : error LNK2019: unresolved external symbol _pcre_free referenced in function "private: void __thiscall pcrecpp::RE::Cleanup(void)" (?C leanup@RE@pcrecpp@@AAEXXZ) pc.exe : fatal error LNK1120: 5 unresolved externals
Any idea what am I doing wrong? Tried also under VC++ 10, same errors.
Should I include <pcre.h>
and #pragma comment(lib,"pcre.lib")
because if I do so the link occurs without any error but seem strange to use the C header and library in the C++ code?
UPDATE:
I did this, and now works, the only problem is: is this ok for C++?
#define PCRE_STATIC 1
#include <pcrecpp.h>
#include <iostream>
#pragma comment(lib,"pcrecpp.lib")
#pragma comment(lib,"pcre.lib")
using namespace std;
int main(void)
{
pcrecpp::RE regex("(hello)");
std::string strBase = "hello hello hello";
pcrecpp::StringPiece input(strBase);
std::string match;
int count = 0;
while (regex.FindAndConsume(&input, &match)) {
count++;
std::cout << count << " " << match << std::endl;
}
}
Output:
C:\Documents and Settings\Administrator\coding\pcre>cl.exe /O2 /MD /EHsc pcc.cpp /link /SUBSYSTEM:CONSOLE Kernel32.Lib User32.lib Wininet.lib pcre.lib Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
pc.cpp Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved.
/out:pc.exe /SUBSYSTEM:CONSOLE Kernel32.Lib User32.lib Wininet.lib pcre.lib get_all_proc.obj
Solution