如何在 Visual Studio 中添加 C++ 项目引用?

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

How do you add a C++ project reference in Visual Studio?

c++visual-studioproject

提问by lucidquiet

In C# it's pretty simple to add a project reference that will build the dependency, put the resulting assembly in the original's Debug/ directory and properly compile against that assembly.

在 C# 中,添加将构建依赖项的项目引用非常简单,将生成的程序集放在原始的 Debug/ 目录中并针对该程序集正确编译。

So at the moment, I have a project with the Main() and a static library project in one solution. However, when I compile the Main() project and I look in the bin/Debug/ directory I don't find either the Static.lib file or the .obj files, which I think would need to be there, or something... I'm getting linker errors.

所以目前,我在一个解决方案中有一个包含 Main() 的项目和一个静态库项目。但是,当我编译 Main() 项目并查看 bin/Debug/ 目录时,我没有找到 Static.lib 文件或 .obj 文件,我认为它们需要在那里,或者其他东西.. . 我收到链接器错误。

I think maybe I have to Configure the project to find the .obj and the .lib file produced by the static library project (which compiles fine, and actually produces those files.)

我想也许我必须配置项目才能找到静态库项目生成的 .obj 和 .lib 文件(它编译得很好,实际上生成了这些文件。)

I'm missing something, and I'm not very well versed in Visual Studio with C++.

我遗漏了一些东西,而且我不太熟悉使用 C++ 的 Visual Studio。

How do I add the reference in the Main project to use the library produced by the static library project?

如何在Main项目中添加引用以使用静态库项目生成的库?

回答by MFH

The first thing you'll have to unterstand is, that static libraries are nothing like .NET assemblies. They are linked into the .exe and are not distributed as separate entity.

您首先必须要理解的是,静态库与 .NET 程序集完全不同。它们链接到 .exe 并且不作为单独的实体分发。

Your linker errors are most likely a result of you not linking to the library.

您的链接器错误很可能是您未链接到库的结果。

There are several ways to define libraries that have to be linked. One is int the project settings under linker -> input -> additional dependencies, the other would be the cheap route via #pragma comment(lib, "path to library")

有多种方法可以定义必须链接的库。一个是链接器->输入->附加依赖项下的项目设置,另一个是通过的廉价路线#pragma comment(lib, "path to library")

回答by Cosima

Here is a very comprehensive answer: visual c++: #include files from other projects in the same solution

这是一个非常全面的答案:visual c++: #include files from other projects in the same solution

It describes the settings for the linker but also other essentials when referencing another C++ project. (especially when coming from C# but not being too versed in C++)

它描述了链接器的设置以及引用另一个 C++ 项目时的其他基本信息。(尤其是来自 C# 但不太精通 C++ 时)

回答by étienne

You can add the name of the .lib files you need by going in project property->Linker->Input->Additional Dependencies

您可以通过进入项目属性->链接器->输入->附加依赖项来添加所需的.lib文件的名称

Then you will have to give the folder where your library is in VC++ Directories->Library Directerories.

然后,您必须在 VC++ 目录-> 库目录中提供您的库所在的文件夹。