本文介绍了使用 Visual Studio 链接到第 3 方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Visual Studios 2013 创建一个 .dll.该项目包括 libpq 功能.

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

根据我在 Internet 上找到的其他 stackoverflow 帖子和其他来源,我(据我所知)已正确地将 postgres 库和包含目录添加到项目中.但是,当我开始构建项目时,它会返回许多未解析的外部符号"错误.

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.

我的路径是 C:Program FilesPostresSQL9.3... 所以我在附加库/包含目录字段中用引号将它们括起来.我已经在项目中包含了 libpq-fe.h 头文件......我只是不确定我做错了什么.

My paths are C:Program FilesPostresSQL9.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.

另一个注意事项,我可以使用带有 -I、-L 和 -lpq 标志的 g++ 从命令行编译测试程序,但我不确定如何从命令行编译为 .dll(加上它增加了我不想处理的复杂性).

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	ills13documentsvisual studio 2013Projectssql_armaReleasesql_arma.dll : fatal error LNK1120: 4 unresolved externals

我已经按照下面的建议在我的项目的源文件中包含了 #pragma comment(lib, "libpq.lib"),但我仍然收到这些错误.

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

推荐答案

我已经成功编译了 示例程序 通过设置这些项目属性:

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

  • includelib 添加到 VC++ Directory->Include->Library,相应地
  • libpq.lib 添加到 Linker->Input->Additional dependencies
  • Add <pgsql install path>include and lib to VC++ Directories->Include and ->Library, correspondingly
  • Add libpq.lib to Linker->Input->Additional dependencies

这是引用第 3 方库的标准方法.只是他们建议为他们的基本目录"使用环境变量,以避免在 VCS 下修补项目.

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.

  • 为了能够从 VS 运行应用程序(有和没有调试),我还在 Debugging- 中指定了 PATH=%PATH%;<pgsql install path>in>环境,因为这个目录不在我系统的 PATH 中.
  • To be able to run the app from VS (both with and without debugging), I also specified PATH=%PATH%;<pgsql install path>in in Debugging->Environment since this dir isn't in PATH on my system.

这篇关于使用 Visual Studio 链接到第 3 方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:41