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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:45:18  来源:igfitidea点击:

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

c++visual-studio-2008visual-c++visual-studio-2005

提问by leeand00

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 动态库,但我仍然不完全确定如何将它们添加到构建中并使它们工作。如何解决这个问题?

采纳答案by Tim

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

我认为您可能会问如何在 IDE 中向项目/解决方案添加库的机制...

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.

请原谅这篇文章的随意性。我认为这就是你想要的。

回答by

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 interfaceof 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 liband the dynamic libraries into binsubdirectories 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 库是完全不同的事情),但是供应商必须为每种构建类型(发布、调试、32 位、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++ 中的等效项访问。

回答by Dan

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 文件。

回答by user893764

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 :)

对于添加库,这非常简单(如果这就是您的意思)项目 -> 属性 -> 配置属性 -> 链接器 -> 输入 -> 附加库。站在那里的图书馆之一,然后按回车键。你腾出了一个空间来添加一个库,就像馅饼一样:)

回答by Mostafa Man

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 允许您将目录添加到其搜索所需库的默认目录中。

回答by plinth

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 编写的,并链接到非托管静态库中。我们发现这是最容易维护的,因为非托管库变化最少。