使用Visual Studio链接到第三方库 [英] Link against a 3rd-party library with Visual Studio

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

问题描述

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

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

对于其他stackoverflow帖子以及我在Internet上找到的其他来源,我(据我所知)已正确添加了postgres lib并在项目中包含目录.但是,当我去构建项目时,它返回许多未解决的外部符号"错误.

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 Files\PostresSQL\9.3\...,所以我在附加库/包含目录"字段中将它们用引号引起来.我已经在项目中包含了libpq-fe.h头文件...我只是不确定自己在做什么错.

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.

另一个说明,我可以使用带有-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\tills13\documents\visual studio 2013\Projects\sql_arma\Release\sql_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:

  • 分别将<pgsql install path>\include\lib添加到 VC ++目录->包含->库
  • libpq.lib添加到 Linker-> Input->其他依赖项
  • Add <pgsql install path>\include and \lib to VC++ Directories->Include and ->Library, correspondingly
  • Add libpq.lib to Linker->Input->Additional dependencies

这是引用第三方库的标准方法.只是他们建议使用环境变量作为其基本目录",以避免在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-> Environment 中指定了PATH=%PATH%;<pgsql install path>\bin,因为该目录不在我的PATH中系统.
  • 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆