Link against a 3rd-party library with Visual Studio

寵の児 提交于 2019-11-26 23:40:12

问题


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.


回答1:


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.



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/25611998/link-against-a-3rd-party-library-with-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!