1

I'm trying to create a .dll with Visual Studios 2013. The project includes libpq functionality.

Per other stackoverflow posts, and other sources I've found on the internet, I've (as far as I'm aware) correctly added the postgres lib and include directories to the project. However, when I go to build the project, it returns a number of "unresolved external symbol" errors.

My paths are C:\Program Files\PostresSQL\9.3\... so I have them surrounded by quotation marks in the Additional Library/Include Directory fields. I've included the libpq-fe.h header file in the project... I'm just not sure what I'm doing wrong.

Another note, I can compile a test program from the command line using g++ with the -I, -L, and -lpq flags, but I'm not sure how to compile to a .dll from the command line (plus it adds complexity that I just don't want to deal with).

These are the specific errors I'm getting:

1>sql_arma.obj : error LNK2001: unresolved external symbol _PQconnectdb
1>sql_arma.obj : error LNK2001: unresolved external symbol _PQstatus
1>sql_arma.obj : error LNK2001: unresolved external symbol _PQerrorMessage
1>sql_arma.obj : error LNK2001: unresolved external symbol _PQfinish
1>C:\Users\tills13\documents\visual studio 2013\Projects\sql_arma\Release\sql_arma.dll : fatal error LNK1120: 4 unresolved externals

I have, as suggested below, included #pragma comment(lib, "libpq.lib") in the source file for my project, I still receive these errors.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62
  • In order to build a DLL that depends on symbols from another DLL, I think you are supposed to export those symbols. Maybe this could help you more: [1] http://msdn.microsoft.com/en-us/library/z4zxe9k8.aspx [2] http://stackoverflow.com/questions/225432/export-all-symbols-when-creating-a-dll [3] http://msdn.microsoft.com/en-us/library/a90k134d.aspx – hbobenicio Sep 01 '14 at 20:18
  • I'm exporting the symbols that I need to in my code. My issue is with undefined symbols from libpq. – Tyler Sebastian Sep 01 '14 at 20:24
  • What about the linking configurations... are you linking against the correct .lib file? Silly question here: those unresolved external symbols are symbols from libpq, right? – hbobenicio Sep 01 '14 at 21:08
  • 1
    @hbobenicio like I said in the post, I can compile it using the command line to an executable which works just fine using the same lib and include dirs – Tyler Sebastian Sep 02 '14 at 01:02
  • 1
    Have you checked the bitness 32/64 and/or the [runtime type](http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx)? – Marco A. Sep 04 '14 at 07:52
  • 2
    @TylerSebastian Can you post the exact linker errors ? This might help us understand the issue clearly. Just shooting in the dark though, are you linking to the correct runtime? – s_b Sep 08 '14 at 17:51
  • @s_b the exact errors, as I see them in the status window are "unresolved external symbol: " If it's available, I have not enabled verbose mode. – Tyler Sebastian Sep 10 '14 at 22:33
  • what's the linker's command line as printed in the build log? I'd like to see if it references the `libpq.lib`. – ivan_pozdeev Sep 11 '14 at 10:22

3 Answers3

3

I've successfully compiled the sample program by setting these project properties:

  • Add <pgsql install path>\include and \lib to VC++ Directories->Include and ->Library, correspondingly
  • Add libpq.lib to Linker->Input->Additional dependencies

This is the standard way to reference 3rd-party libs. It's just that they recommend using environment variables for their "base dirs" to avoid patching the project when it's under a VCS.

  • To be able to run the app from VS (both with and without debugging), I also specified PATH=%PATH%;<pgsql install path>\bin in Debugging->Environment since this dir isn't in PATH on my system.
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Thank you. That adding `libpq.lib` to Additional Dependencies did it. I wish I could have given you the bounty. – Tyler Sebastian Sep 14 '14 at 00:10
  • @ivan_pozdeev: I have added pgsql path of include and lib dir in VC++ directories. Also added libpq.lib as an additional dependencies still I am seen linking issues as unresolved symbol. Code is compiling successfully but giving linking error. – void Jan 14 '15 at 14:47
  • @void It's impossible to tell without seeing more info. Ask another question mentioning you did as this one specifies and provide related settings and output. – ivan_pozdeev Jan 15 '15 at 11:34
  • @ivan_pozdeev: I could able to solve linking issue. I was using 64 bit postgres installer as I have 64bit OS when I replaced 64bit version with 32bit. I was able to run the sample code successfully. I am not sure why it was giving me linking errors for 64 bit installer. – void Jan 19 '15 at 07:03
0

It's not sufficient add the postgres lib directory to the project, you must also add reference to libpq.lib. Just add this line to one of your source .cpp files:

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

As noted by Marco A. the library must match a program bitness (32 or 64 bit): if you build 32-bit DLL (referred as Win32) you must use 32 bit library; if 64-bit (x64) - 64-bit library.

Rimas
  • 5,904
  • 2
  • 26
  • 38
0

I have also faced same issue. Then I realized that I was building my application as a 32bit. I changed the target of my application to x64 and it compiled successfully

enter image description here

user2807083
  • 2,962
  • 4
  • 29
  • 37