visual-studio 如何在 C++ 中“添加引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1127460/
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-10-22 10:46:21  来源:igfitidea点击:

How to "add reference" in C++

c++visual-studiodll

提问by John M Gant

I'm new to C++ and there's something I just completely don't get. In C#, if I want to use an external library, log4net for example, I just add a reference to the log4net DLL and its members are automatically available to me (and in IntelliSense). How do I do that in non-managed C++?

我是 C++ 的新手,有些东西我完全不明白。在 C# 中,如果我想使用外部库,例如 log4net,我只需添加对 log4net DLL 的引用,它的成员将自动可供我使用(以及在 IntelliSense 中)。我如何在非托管 C++ 中做到这一点?

回答by jalf

Often, the library comes with 1) a header file (.h) and 2) a .lib file in addition to the .dll.

通常,除了 .dll 之外,该库还附带 1) 头文件 (.h) 和 2) .lib 文件。

The header file is #include'ed in your code, to give you access to the type and function declarations in the library.

头文件在您的代码中被#include,以便您访问库中的类型和函数声明。

The .lib is linked into your application (project properties -> linker -> input, additional dependencies).

.lib 链接到您的应用程序(项目属性 -> 链接器 -> 输入,附加依赖项)。

The .lib file usually contains simple stubs that automatically load the dll and forward function calls to it.

.lib 文件通常包含自动加载 dll 并将函数调用转发给它的简单存根。

If you don't have a .lib file, you'll instead have to use the LoadLibrary function to dynamically load the DLL.

如果您没有 .lib 文件,则必须使用 LoadLibrary 函数来动态加载 DLL。

回答by ovanes

The basic concept is the following: There are 2 types of libraries: static & dynamic. The difference between them is that static libraries, during the linking build step, embed their compiled code in your executable (or dll); dynamic libs just embed pointers to the functions and instructions that some dll should be loaded when program is going to be loaded. This is realized for you by the linker.

基本概念如下: 有两种类型的库:静态和动态。它们之间的区别在于静态库在链接构建步骤期间将它们编译的代码嵌入到您的可执行文件(或 dll)中;动态库只是嵌入了指向函数和指令的指针,当程序将要加载时,应该加载一些 dll。这是由链接器为您实现的。

Now you can decide which of those two you are going to use. DLLs have many advantages and disadvantages. If developing a huge application it might be worthy to consider using DLLs with delay loading instead of static lib's. Some libs are simply delivered to you as DLLs and you have no choice. Anyway the easiest way for a beginner would be to use static libraries. That would make your deployment and test much easier, since, when dealing with DLL you have to ensure that they are found at runtime (even when using debugger), this involves either copying everything in one directory or dealing with path variables.

现在您可以决定要使用这两个中的哪一个。DLL 有许多优点和缺点。如果开发大型应用程序,可能值得考虑使用具有延迟加载的 DLL 而不是静态库。一些库只是作为 DLL 交付给您,您别无选择。无论如何,初学者最简单的方法是使用静态库。这将使您的部署和测试更加容易,因为在处理 DLL 时,您必须确保在运行时找到它们(即使使用调试器),这涉及将所有内容复制到一个目录中或处理路径变量。

Usually a DLL provider (if it is intended that you should be able to deal with the library) delivers you a header file(s) and a .lib which contains the calls into the desired DLL. Some vendors (e.g. boost) only require you to include the header file and the lib is automatically linked to your executable (can be achieved through compiler prorietary pragma directive). If it is not the case you must go into the project settings of the C++ project (project properties/Configuration Properties/Linker/Input) and enter the lib file name into the "Additional Dependencies" row, e.g. iced.lib; iceutild.lib. You can also put fully qualified path names there. Be aware that you have to enter the lib file names for both configurations (Debug, Release). This is the procedure you do with static libraries and Dll equally. The only difference that DLL will require a DLL lib to be either in you app-directory or in one of the path-directories.

通常,DLL 提供程序(如果打算让您处理该库)会向您提供一个头文件和一个 .lib,其中包含对所需 DLL 的调用。某些供应商(例如 boost)只要求您包含头文件,并且 lib 会自动链接到您的可执行文件(可以通过编译器专有 pragma 指令实现)。如果不是这种情况,您必须进入 C++ 项目的项目设置(项目属性/配置属性/链接器/输入)并将 lib 文件名输入到“附加依赖项”行中,例如iced.lib; iceutild.lib. 您还可以将完全限定的路径名​​放在那里。请注意,您必须为两种配置(调试、发布)输入 lib 文件名。这是您对静态库和 Dll 同样执行的过程。唯一的区别是 DLL 需要 DLL 库位于您的应用程序目录或路径目录之一中。

After that step, you still might get compiler errors if you try to link incompatible libraries. There are many reasons, why they can be incompatible. But try to first link the lib this way and see if works. If not, post again your errors here ;)

在该步骤之后,如果您尝试链接不兼容的库,您仍然可能会遇到编译器错误。有很多原因,为什么它们会不兼容。但是尝试首先以这种方式链接库,看看是否有效。如果没有,请在此处再次发布您的错误;)

Include file(s) is(are) used to be included in places, where you would like to use smth. from the lib. Just include it and the compiler will know that the symbols must come either from another (compiled) compilation unit (compiled cpp-file=>object file) or the .lib. It will make the look up and notify you if the required symbols are not found.

包含文件过去常常包含在您想使用 smth 的地方。从库。只需包含它,编译器就会知道符号必须来自另一个(已编译的)编译单元(已编译的 cpp-file=>object 文件)或 .lib。如果找不到所需的符号,它将进行查找并通知您。

Good Luck, Ovanes

祝你好运,欧文斯

P.S. This might be hard in the beginning, but when you get used to it, it will be easy.

PS 刚开始可能会很难,但是习惯了之后就很容易了。

回答by Daniel Earwicker

C++ doesn't have libraries in the sense you're thinking of. It has header files that you #include, and it has things called libraries that the linker deals with, which contain the compiled code. You need to add the libraries (.LIB files) to the linker settings.

C++ 没有你想象中的库。它有你的头文件,#include它有链接器处理的称为库的东西,其中包含已编译的代码。您需要将库(.LIB 文件)添加到链接器设置中。

On Windows if you're using a DLL, ideally you should have a .LIB file to go with it that is called the Import Library for the DLL, and you add that .LIB file to your linker settings.

在 Windows 上,如果您使用的是 DLL,理想情况下您应该有一个 .LIB 文件与之配套,称为 DLL 的导入库,然后将该 .LIB 文件添加到链接器设置中。

回答by bobobobo

The first thing you need to do is to #include the header file that describes the functions that are available in that library.

您需要做的第一件事是#include 描述该库中可用函数的头文件。

The actual code for the library will be in one of 2 places:

库的实际代码将位于以下两个位置之一:

  1. A static library (.lib)
  2. A dll (.dll)
  1. 静态库 (.lib)
  2. 一个 dll (.dll)

Depending on how the library's code is given to you (as .lib files, or as a .dll), you'll have to either:

根据提供给您的库代码的方式(作为 .lib 文件或作为 .dll),您必须:

  • #pragma comment( lib, "libraryname.lib" ) if its a .lib
  • LoadLibraryif its a .dll
  • #pragma comment( lib, "libraryname.lib" ) 如果它是 .lib
  • LoadLibrary如果它是一个 .dll

Sometimes a package comes with BOTH a .lib file that you need to link to, and a .dll file. In this case you don't need to call LoadLibrary, you only need to #pragma comment( lib, "libaryfile.lib" ) because in this case the .lib links you into the .dll.

有时,一个包同时带有您需要链接到的 .lib 文件和 .dll 文件。在这种情况下,您不需要调用 LoadLibrary,您只需要 #pragma comment( lib, "libaryfile.lib" ) 因为在这种情况下 .lib 将您链接到 .dll。

A very important detail is to put the DLL where your application can find it. Charles Petzold says:

一个非常重要的细节是将 DLL 放在您的应用程序可以找到它的地方。查尔斯·佩佐德 说:

When Windows needs to load a DLL module before running a program that requires it, the library file must be stored in the directory containing the .EXE program, the current directory, the Windows system directory, the Windows directory, or a directory accessible through the PATH string in the MS-DOS environment. (The directories are searched in that order.) Programming windows, 5th edMSDN
当 Windows 需要在运行需要它的程序之前加载一个 DLL 模块时,库文件必须存储在包含 .EXE 程序的目录、当前目录、Windows 系统目录、Windows 目录或通过MS-DOS 环境中的 PATH 字符串。(按该顺序搜索目录。) Programming windows,第 5 版MSDN

I don't recommend using the project properties menu to link because it isn't as visible what libraries you're linking to.

我不建议使用项目属性菜单进行链接,因为您链接到的库并不可见。

See also

也可以看看