1

I use Qt5, msvc2010, windows7 64bit.

I want to test if i can link libpq.lib.

http://www.postgresql.org/docs/9.2/static/libpq-example.html

I build the above example in QtCreator.

And get

error LNK2019: unresolved external symbol _PQconnectdb in function "_main"

I check my libpq.lib use dumpbin libpq.lib /exports

And get

        1    PQconnectdb
      156    PQconnectdbParams

How to See the Contents of Windows library (*.lib)

so difference is PQconnectdb and _PQconnectdb.

Is that underscore that makes the linker can't find the real symbol PQconnectdb? Why the compiler add an underscore to the symbol?

How can I solve this problem?

Community
  • 1
  • 1
Aylwyn Lake
  • 1,919
  • 4
  • 26
  • 36
  • Dump question may be... but have you included libpq.lib in your project? – Digital_Reality Jan 03 '14 at 10:17
  • Yes. I add `LIBS += -L$$PWD libpq.lib` in the .pro file. – Aylwyn Lake Jan 03 '14 at 14:52
  • Have you tried LIBS += -L$$PWD -lpq or LIBS += -L$$PWD -llibpq? Btw, you do not really need to specify PWD library paths. Windows takes care of that automatically. – László Papp Jan 03 '14 at 15:28
  • Does it work if you try to build it with dynamic linkage? Could you please show your whole qmake project file? – László Papp Jan 03 '14 at 16:29
  • If you got `libpq.lib` from entreprisedb (either zip or installer), it seems that 64 bits versions don't have underscores before symbols whereas 32 bits versions do. Maybe your project is somehow set to 32 bits and you're linking with the 64 bits libpq.lib? – Daniel Vérité Jan 03 '14 at 17:16
  • Related to the previous comment: [MS VC linker (link.exe): Why no warning for 32/64 bit CPU architecture mismatch?](http://stackoverflow.com/questions/7938936) – Daniel Vérité Jan 03 '14 at 17:40
  • Have you checked this? http://stackoverflow.com/questions/15809204/access-postgresql-via-c-interface-linker-error – László Papp Jan 04 '14 at 00:02
  • @DanielVérité: Thank you very much for the knowledge of which `64 bits versions don't have underscores before symbols whereas 32 bits versions do.` – Aylwyn Lake Jan 06 '14 at 01:31

1 Answers1

3

Is that underscore that makes the linker can't find the real symbol PQconnectdb? Why the compiler add an underscore to the symbol?

This is the __cdecl convention that is still in effect for x86 (32 bits) but has been obsoleted for 64 bits builds.

Since dumpbin libpq.lib /exports shows no underscores, it means that this library comes from a 64 bits build.

To produce a 32 bits program, replace your libraries with the lib directory from a 32 bits PostgreSQL zip archive. The contents will be compatible with your current build configuration that appears to be 32 bits.

On the other hand, to produce a 64 bits program, configure Qt Creator to use a 64 bits "Kit" (e.g. in Qt Creator 3, Projects tab, see Add Kit in the Build & Run panel) with the 64 bits PostgreSQL libraries you already have.

Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
  • I am wondering why you post a reply if you yourself point that out this is a duplicate? You could use the duplicate flag for question that you reveal as duplicate. I would personally prefer not to duplicate the answers, too. – László Papp Jan 04 '14 at 00:55
  • @LaszloPapp: actually you found the duplicate, not me. Now I agree this is clearly the same error and cause, yet the dup doesn't care about why the underscore, and it's aimed at a 64 bits build with no interest for the 32 bits flavor, which I'm not sure is the case here. – Daniel Vérité Jan 04 '14 at 01:59
  • IMO, even your link explains the issue. Even if not necessarily the exact same lib in question, the root cause is still the same. :) – László Papp Jan 04 '14 at 02:00
  • @LaszloPapp: Yes. Actually I've found that link. I use 32 bits msvc2010 in my qtcreator. I've download both 32 bits and 64 bits postgresql installer and installed them, I checked the error by using both libpq.lib, if I remember correctly. Any way, I'll try again after I'm back to work. – Aylwyn Lake Jan 05 '14 at 08:26
  • @LaszloPapp: I just test. It is wrong lib. a 32 bit lib is ok. By the way, when use dumpbin /headers to see lib is 32 bit or 64 bit, FILE HEADER VALUES: 14c for x86, 8664 for x64 – Aylwyn Lake Jan 06 '14 at 01:28