如何在VC ++中添加外部库进行编译?

[英]How do you add external libraries for compilation in VC++?


I've worked with a couple of Visual C++ compilers (VC97, VC2005, VC2008) and I haven't really found a clearcut way of adding external libraries to my builds. I come from a Java background, and in Java libraries are everything!

我曾经使用过几个Visual C ++编译器(VC97,VC2005,VC2008),我还没有真正找到一种向我的构建添加外部库的清晰方法。我来自Java背景,在Java库中是一切!

I understand from compiling open-source projects on my Linux box that all the source code for the library seems to need to be included, with the exception of those .so files.

我理解在Linux机器上编译开源项目时,除了那些.so文件之外,似乎需要包含库的所有源代码。

Also I've heard of the .lib static libraries and .dll dynamic libraries, but I'm still not entirely sure how to add them to a build and make them work. How does one go about this?

我也听说过.lib静态库和.dll动态库,但我仍然不完全确定如何将它们添加到构建中并使它们工作。怎么会这样呢?

6 个解决方案

#1


7  

In I think you might be asking the mechanics of how to add a lib to a project/solution in the IDEs...

我想你可能会问如何在IDE中为项目/解决方案添加lib的机制......

In 2003, 2005 and 2008 it is something similar to:

在2003年,2005年和2008年,它类似于:

from the solution explorer - right click on the project select properties (typically last one) I usually select all configurations at the top... Linker Input

从解决方案资源管理器 - 右键单击​​项目选择属性(通常是最后一个)我通常选择顶部的所有配置...链接器输入

Additional dependencies go in there

其他依赖关系在那里

I wish I could do a screen capture for this.

我希望我能为此做一个屏幕截图。

In VC6 it is different bear with me as this is all from memory

在VC6中,它与我不同,因为这完全来自记忆

project settings or properties and then go to the linker tab and find where the libs can be added.

项目设置或属性,然后转到链接器选项卡,找到可以添加库的位置。

Please excuse the haphazard nature of this post. I think that is what you want though.

请原谅这篇文章的偶然性质。我想这就是你想要的。

#2


15  

Libraries in C++ are also considered helpful, but the way you integrate them is different to Java because the compiler only has to see the interface of the library, which is usually declared in header files. In Java, the compiler will have to inspect the actual libraries because Java doesn't have this distinction between an externally visible header file and the generated object code providing the implementation.

C ++中的库也被认为是有用的,但是它们集成它们的方式与Java不同,因为编译器只需要查看库的接口,该接口通常在头文件中声明。在Java中,编译器必须检查实际的库,因为Java在外部可见的头文件和提供实现的生成的目标代码之间没有这种区别。

What you normally do is build the libraries separately, once, and put the generated lib/dll files plus the header files into a place that projects requiring the library can access. A common idiom is to put the header files into include, the static libraries into lib and the dynamic libraries into bin subdirectories for your compiled library.

您通常做的是单独构建库,一次,并将生成的lib / dll文件和头文件放入需要库可以访问的项目的位置。一个常见的习惯用法是将头文件放入include,将静态库放入lib,将动态库放入bin子目录中,用于编译库。

The reason you have found that most C++ libraries are provided in source code form and not in precompiled form is that every C++ compiler has a certain freedom as to how to mangle symbol names etc and the resulting object code isn't portable across compilers, let alone operating systems. So shipping the compiled code doesn't make sense for a lot of applications. You'll occasionally find it with closed-source C++ libraries on Windows (C libraries are an entirely different matter), but then the vendor will have to provide a compiled version for each and every build type (Release, Debug, 32 bit, 64 bit etc) and target compiler (various versions of Visual Studio require different binaries, then there is Borland and a bunch of other compilers) and it quickly becomes a nightmare to support...

您发现大多数C ++库以源代码形式提供而不是以预编译形式提供的原因是每个C ++编译器都有一定的自由来如何破坏符号名称等,并且生成的对象代码不能跨编译器移植,让单独的操作系统。因此,运送已编译的代码对于许多应用程序来说没有意义。您偶尔会在Windows上使用闭源C ++库找到它(C库是完全不同的事情),但供应商必须为每种构建类型提供编译版本(Release,Debug,32 bit,64比特等)和目标编译器(各种版本的Visual Studio需要不同的二进制文件,然后是Borland和一堆其他编译器),它很快成为支持的噩梦......

When you take a library and build it as a dynamic library on Windows (ie, a DLL), the compiler/linker will normally generate a static 'import' library for it (same name, just with a .lib extension). When you link your project against the dynamic library, you specify the .lib file as a library dependency. Linking your application against said import library allows the linker to record the dependency on the .dll file and also which symbols it should expect the library to provide.

当您使用库并将其构建为Windows上的动态库(即DLL)时,编译器/链接器通常会为其生成一个静态的“导入”库(同名,只有.lib扩展名)。将项目与动态库链接时,将.lib文件指定为库依赖项。将应用程序与所述导入库链接允许链接器记录对.dll文件的依赖性以及它应该期望库提供的符号。

Making them work - in the sense of your program finding them on Windows - usually requires that the .dll file is either in the same directory as the executable or accessible via the 'PATH' environment variable and its equivalent in Visual C++.

使它们工作 - 在程序中在Windows上找到它们 - 通常要求.dll文件与可执行文件位于同一目录中,或者通过'PATH'环境变量及其在Visual C ++中的等效文件可访问。

#3


8  

Ok. typically you don't want to load dynamic libraries by hand, but if you do, look into LoadLibrary. you then have to call other functions to get function pointer addresses and so forth. Typically how it works, is even .dll files have .lib files for them.. so when they are needed, they just automatically load.

好。通常,您不希望手动加载动态库,但如果您这样做,请查看LoadLibrary。然后你必须调用其他函数来获取函数指针地址等等。通常它是如何工作的,甚至.dll文件都有.lib文件。所以当需要它们时,它们只是自动加载。

how you specifiy .lib files /static libraries is under Properties/Linker/Input->Additional Dependencies. in the gui.

如何指定.lib文件/静态库位于Properties / Linker / Input-> Additional Dependencies下。在gui。

if you are scripting this.. well. you just specifiy the .lib files on the command line at link time.

如果你正在编写脚本...好吧。您只需在链接时指定命令行上的.lib文件。

#4


3  

For adding libraries, this is very simple (if that's what you mean) Project -> properties -> configure properties -> Linker -> Input -> additional libraries. Go stand on one of the libraries in there and press enter. You freed up a space to add a library, eay as pie :)

对于添加库,这非常简单(如果这就是你的意思)项目 - >属性 - >配置属性 - >链接器 - >输入 - >其他库。站在其中一个库上,按回车键。你释放了一个空间来添加一个库,就像馅饼一样:)

#5


2  

For adding external libraries or library directories in a VC++ project follow these steps:
1- Go to your project properties (that can be done by right clicking on the project in your solution)
2- Select 'Linker' > 'General' > Additional Library Directories
3- Click on the empty bar in front of 'Additional Library Directories' and VS allows you to add directories to its default ones into which it searches for the required libraries.

要在VC ++项目中添加外部库或库目录,请执行以下步骤:1-转到项目属性(可以通过右键单击解决方案中的项目来完成)2-选择“链接器”>“常规”>附加库目录3-单击“附加库目录”前面的空白栏,VS允许您将目录添加到其中搜索所需库的默认目录。

#6


0  

What do you mean "add them to a build"?

你是什​​么意思“将它们添加到构建中”?

In VC, within a solution, you can have a project whose output is a library (static or dynamic) and then another project that depends upon the output and uses it (ie, links to it).

在VC中,在一个解决方案中,您可以拥有一个项目,其输出是一个库(静态或动态),然后是另一个依赖于输出并使用它的项目(即链接到它)。

In our code base, we typically have a separate solution for each library or set of libraries and then link them into target projects. Most of our target projects are managed assemblies - those that need unmanaged functionality are usually written in managed C++ or C++/CLI and link in unmanaged static libraries. We've found that this has been the easiest to maintain since the unmanaged libraries change the least.

在我们的代码库中,我们通常为每个库或一组库提供单独的解决方案,然后将它们链接到目标项目中。我们的大多数目标项目都是托管程序集 - 那些需要非托管功能的程序通常用托管C ++或C ++ / CLI编写,并链接到非托管静态库中。我们发现这是最容易维护的,因为非托管库的变化最小。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2008/10/17/917c90cd093f9bea2c08b20b01397852.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告