Discussion:
PCRE & MFC: problem with link error
(too old to reply)
L.Allan
2007-12-10 18:54:22 UTC
Permalink
I'm trying to get an MFC v6 project to link with the C Perl Compatible
Regular Expression (pcre) library, and encountering a link error about
unresolved references. Pcre seems mostly oriented to the unix/linux
world, and more or less unaware that Microsoft exists. There is a C++
"wrapper" but it seems mostly for having a regex object, which isn't
what I'm struggling with. Help appreciated.

I'm mostly ignorant about using the linker beyond using the ide.

extern "C" {
#include "pcre.h"
}
BOOL CPcreTestApp::InitInstance()
{
pcre *re = NULL;
const char* error;
int errorOffset;
char *subject = "Is the string red found in this string?";
int ovector[30];
int subject_length = strlen(subject);
const char* version = pcre_version();
re = pcre_compile("red.+this", 0, &error, &errorOffset, NULL);
int rc = pcre_exec(re, NULL, subject, subject_length, 0, 0, ovector,
30);
.... MFC code
}

The above compiles ok, but results in the following link errors:
PcreTest.obj : error LNK2001: unresolved external symbol
__imp__pcre_exec
PcreTest.obj : error LNK2001: unresolved external symbol
__imp__pcre_compile
PcreTest.obj : error LNK2001: unresolved external symbol
__imp__pcre_version

The pcre library comes with several .def files, but the various
combinations I've tried don't help.

I've got pcre_vc6d.lib specified in the .dsp file, but it isn't able
to resolve. I've been able to get it to compile/link/run as a
"vanilla" C project with PcreTestConsole.c linking to
Debug\pcre_vc6d.lib, so building the library seems to work.

Do I need something like pcre_mfc.def and, if so, what would it
contain? Is there some parameter to the compiler or linker I need?
Should I build the pcre_vc6d.lib differently?

Here is a link to the vc6 project:
http://www.berbible.org/misc/PcreTest.zip
Giovanni Dicanio
2007-12-11 11:30:02 UTC
Permalink
Post by L.Allan
I'm trying to get an MFC v6 project to link with the C Perl Compatible
Regular Expression (pcre) library, and encountering a link error about
unresolved references. Pcre seems mostly oriented to the unix/linux world,
and more or less unaware that Microsoft exists.
I don't think so.
Examining the 'pcre.h" header file, it seems that its authors had also
considered the Windows / Win32 system and Win32 DLLs scenarios (you can see
that reading some preprocessor #ifdef... at the beginning of the header
file).
Post by L.Allan
extern "C" {
#include "pcre.h"
}
You don't need the 'extern "C"' here, because the header file pcre.h takes
care of that (it has the classical C++ 'extern "C"' "guard" in it).
Post by L.Allan
PcreTest.obj : error LNK2001: unresolved external symbol __imp__pcre_exec
I belive you must use the .lib file for pcre.
Post by L.Allan
The pcre library comes with several .def files, but the various
combinations I've tried don't help.
I think what you need here is the .lib file (and the DLLs, of course), not
the .def file.
Post by L.Allan
I've got pcre_vc6d.lib specified in the .dsp file, but it isn't able to
resolve.
Please try to put the following line in MFC source file that you quoted
above (after the #includes):

#pragma comment( lib, "pcre_vc6.lib" )

and you should also put the file "pcre_vc6.lib" (the "bridge" to call the
DLL functions) in your project folder.

HTH,
Giovanni
Lynn Allan
2007-12-11 14:03:54 UTC
Permalink
Thanks for the reply, but the problem doesn't seem to be finding the
library, but resolving the name of the imported function that is
exported by the pcre lib. My impression is that I'm leaving something
out that allows the functions names to resolve:
pcre_exec --> __imp__pcre_exec
pcre_compile --> __imp__pcre_compile
pcre_version --> __imp__pcre_version
Post by Giovanni Dicanio
Post by L.Allan
PcreTest.obj : error LNK2001: unresolved external symbol
__imp__pcre_exec
I belive you must use the .lib file for pcre.
Post by L.Allan
The pcre library comes with several .def files, but the various
combinations I've tried don't help.
I think what you need here is the .lib file (and the DLLs, of
course), not the .def file.
Post by L.Allan
I've got pcre_vc6d.lib specified in the .dsp file, but it isn't
able to resolve.
Please try to put the following line in MFC source file that you
#pragma comment( lib, "pcre_vc6.lib" )
and you should also put the file "pcre_vc6.lib" (the "bridge" to
call the DLL functions) in your project folder.
HTH,
Giovanni
David Ching
2007-12-11 16:05:17 UTC
Permalink
Post by Lynn Allan
Thanks for the reply, but the problem doesn't seem to be finding the
library, but resolving the name of the imported function that is exported
by the pcre lib. My impression is that I'm leaving something out that
pcre_exec --> __imp__pcre_exec
pcre_compile --> __imp__pcre_compile
pcre_version --> __imp__pcre_version
Run the cmdline program

dumpbin.exe /exports pcre.lib

(dumpbin comes with Visual Studio)

to see the names of the exported functions.

-- David
Lynn Allan
2007-12-11 22:02:57 UTC
Permalink
Here is the redirected output of:
dumpbin /exports pcrelib_vc6d.lib

Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Dump of file pcrelib_vc6d.lib
File Type: LIBRARY
Summary
4 .bss
5E4C .data
13F19 .debug$S
780 .debug$T
4D8 .drectve
1688 .rdata
44 .rtc$IMZ
44 .rtc$TMZ
11CDC .text

Hmmm .... looks like none of the functions are being exported. Do I
need to revise the declarations within the pcre_somefile.c ... or use
a .def file? If so, how?

Thanks ... seem to be getting closer.
Post by David Ching
Post by Lynn Allan
Thanks for the reply, but the problem doesn't seem to be finding
the library, but resolving the name of the imported function that
is exported by the pcre lib. My impression is that I'm leaving
pcre_exec --> __imp__pcre_exec
pcre_compile --> __imp__pcre_compile
pcre_version --> __imp__pcre_version
Run the cmdline program
dumpbin.exe /exports pcre.lib
(dumpbin comes with Visual Studio)
to see the names of the exported functions.
-- David
David Ching
2007-12-11 23:43:35 UTC
Permalink
Post by Lynn Allan
dumpbin /exports pcrelib_vc6d.lib
Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file pcrelib_vc6d.lib
File Type: LIBRARY
Summary
4 .bss
5E4C .data
13F19 .debug$S
780 .debug$T
4D8 .drectve
1688 .rdata
44 .rtc$IMZ
44 .rtc$TMZ
11CDC .text
Hmmm .... looks like none of the functions are being exported. Do I need
to revise the declarations within the pcre_somefile.c ... or use a .def
file? If so, how?
Thanks ... seem to be getting closer.
Nothing as eye-opening as a dump with nothing in it, huh? ;) See
http://groups.google.com/group/microsoft.public.vc.mfc/msg/32e8558d38619360
for how I like to export functions.

-- David
Lynn Allan
2007-12-12 14:06:58 UTC
Permalink
Thanks for your patient help as I wrestle with the linker/librarian

I got the linker to resolve the references with a pcre.def file that
has this contents:
LIBRARY pcre
EXPORTS
pcre_malloc
pcre_free
pcre_config
pcre_callout
pcre_compile
pcre_compile2
pcre_copy_substring
pcre_dfa_exec
pcre_exec
pcre_get_substring
pcre_get_stringnumber
pcre_get_substring_list
pcre_free_substring
pcre_free_substring_list
pcre_info
pcre_fullinfo
pcre_maketables
pcre_study
pcre_version

However, when I try to run the PcreTest.exe, I get an error message
about "missing pcre.dll".

My preference is to statically link the PcreTest.exe and not have to
distribute the pcre.dll.

In the settings for building pcre.lib, I specified:
Configuration Type: Static Library (.lib).
There are also preprocessor #define's for PCRE_STATIC

However, the generated Release\pcre.lib and Release\PcreTest.exe are
much smaller than expected, which would be the case if a .dll was
required.

I looked though the build configuration settings for PcreTest.exe and
didn't see anywhere that pcre.dll was required. Is the above pcre.def
incorrect?

Here is a link to the vc6 project with source files. It has an Mfc
project and two console projects:
http://www.berbible.org/misc/pcre-74_071212b.zip

Thanks
Post by David Ching
Post by Lynn Allan
dumpbin /exports pcrelib_vc6d.lib
Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file pcrelib_vc6d.lib
File Type: LIBRARY
Summary
4 .bss
5E4C .data
13F19 .debug$S
780 .debug$T
4D8 .drectve
1688 .rdata
44 .rtc$IMZ
44 .rtc$TMZ
11CDC .text
Hmmm .... looks like none of the functions are being exported. Do I
need to revise the declarations within the pcre_somefile.c ... or
use a .def file? If so, how?
Thanks ... seem to be getting closer.
Nothing as eye-opening as a dump with nothing in it, huh? ;) See
http://groups.google.com/group/microsoft.public.vc.mfc/msg/32e8558d38619360
for how I like to export functions.
-- David
David Ching
2007-12-12 23:49:55 UTC
Permalink
Post by Lynn Allan
Thanks for your patient help as I wrestle with the linker/librarian
I got the linker to resolve the references with a pcre.def file that has
LIBRARY pcre
EXPORTS
pcre_malloc
pcre_free
pcre_config
pcre_callout
pcre_compile
pcre_compile2
pcre_copy_substring
pcre_dfa_exec
pcre_exec
pcre_get_substring
pcre_get_stringnumber
pcre_get_substring_list
pcre_free_substring
pcre_free_substring_list
pcre_info
pcre_fullinfo
pcre_maketables
pcre_study
pcre_version
However, when I try to run the PcreTest.exe, I get an error message about
"missing pcre.dll".
My preference is to statically link the PcreTest.exe and not have to
distribute the pcre.dll.
Configuration Type: Static Library (.lib).
There are also preprocessor #define's for PCRE_STATIC
However, the generated Release\pcre.lib and Release\PcreTest.exe are much
smaller than expected, which would be the case if a .dll was required.
I looked though the build configuration settings for PcreTest.exe and
didn't see anywhere that pcre.dll was required. Is the above pcre.def
incorrect?
Here is a link to the vc6 project with source files. It has an Mfc project
http://www.berbible.org/misc/pcre-74_071212b.zip
I have successfully built your source code so that the PcreText.exe uses the
static library. I had to change a few things:

1. PcreTest.dsw - Open the PcreTest.dsw in VC6. In FileView, select
"PcreTest files", then activate the Project |Dependencies menu item, and
make pcrelib a dependency. Not only will this cause the PcreTest.exe to be
rebuilt when anything in the library becomes out of date, it will also make
PcreTest link with the built Pcrelib.lib automatically.

2. PcreTest.dsp and PcreLib.dsp - in compiler defines, add PCRE_STATIC.
This is required so that both the library and the exe see prototypes of the
library functions as non-exported so link errors don't occur.

3. PcreTest.cpp - remove the line:
#pragma comment( lib,
"X:\\LdaFwApps\\BerBible\\Prototypes\\RegEx\\PcreVcProject\\Debug\\pcre.lib"
)

because the pcrelib.lib (and not any file called pcre.lib) will be
automatically linked due to change #1 above.


Please try this and I hope it gets you on your way. :-)


-- David
Lynn Allan
2007-12-13 17:01:23 UTC
Permalink
THANKS! .... works fine now.
Volker Enderlein
2007-12-12 08:12:49 UTC
Permalink
Hi Lynn,
Post by Lynn Allan
dumpbin /exports pcrelib_vc6d.lib
Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file pcrelib_vc6d.lib
File Type: LIBRARY
Summary
4 .bss
5E4C .data
13F19 .debug$S
780 .debug$T
4D8 .drectve
1688 .rdata
44 .rtc$IMZ
44 .rtc$TMZ
11CDC .text
Hmmm .... looks like none of the functions are being exported. Do I
need to revise the declarations within the pcre_somefile.c ... or use
a .def file? If so, how?
Thanks ... seem to be getting closer.
when building the pcre DLL (not a static LIB) and the import library you
need to add PCRE_DEFINITION to the project preprocessor settings. Then
the functions should be exported automagically. You can check this by
opening the DLL with the dependency viewer (depends.exe in the VC6 bin
directory).

Cheers Volker
L.Allan
2007-12-22 23:30:18 UTC
Permalink
Post by Volker Enderlein
when building the pcre DLL (not a static LIB) and the import library you
need to add PCRE_DEFINITION to the project preprocessor settings. Then the
functions should be exported automagically. You can check this by opening
the DLL with the dependency viewer (depends.exe in the VC6 bin directory).
Volker,

Thanks for the info ... I'm statically linking with pcre.lib that was built
with PCRE_STATIC, so it is less applicable.

I tried out your suggestion, and dumpbin /exports can "see" the exports.
I'll add this information about depends.exe and dumpbin to my "tool-box" ...
and hopefully remember it the next time I'm stumped with linker problems.
Loading...