Search code examples
qtpostgresqlqt-creatorlibpq

link to libpq failed with unresolved symbol _PQconnectdb


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?


Solution

  • 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.