Visual C++:#include 来自同一解决方案中其他项目的文件

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

visual c++: #include files from other projects in the same solution

c++visual-studioprojects-and-solutions

提问by rlbond

I am working on a game using Visual C++. I have some components in separate projects, and have set the project dependencies. How do I #include a header file from a different project? I have no idea how to use classes from one project in another.

我正在使用 Visual C++ 开发游戏。我在单独的项目中有一些组件,并设置了项目依赖项。如何#include 来自不同项目的头文件?我不知道如何在另一个项目中使用一个项目中的类。

回答by LeopardSkinPillBoxHat

Settings for compiler

编译器设置

In the project where you want to #include the header file from anotherproject, you will need to add the path of the header file into the Additional Include Directoriessection in the project configuration.

在您想要#include 来自另一个项目的头文件的项目中,您需要将头文件的路径添加到项目配置中的Additional Include Directories部分。

To access the project configuration:

要访问项目配置:

  1. Right-click on the project, and select Properties.
  2. Select Configuration Properties->C/C++->General.
  3. Set the path under Additional Include Directories.
  1. 右键单击该项目,然后选择“属性”。
  2. 选择配置属性->C/C++->常规。
  3. 在附加包含目录下设置路径。

How to include

如何包含

To include the header file, simply write the following in your code:

包含头文件,只需在代码中写入以下内容:

#include "filename.h"

Note that you don't need to specify the path here, because you include the directory in the Additional Include Directories already, so Visual Studio will know where to look for it.

请注意,您无需在此处指定路径,因为您已经在其他包含目录中包含了该目录,因此 Visual Studio 将知道在哪里查找它。

If you don't want to add every header file location in the project settings, you could just include a directory up to a point, and then #include relative to that point:

如果您不想在项目设置中添加每个头文件位置,您可以只包含一个目录直到某个点,然后 #include 相对于该点:

// In project settings
Additional Include Directories    ..\..\libroot

// In code
#include "lib1/lib1.h"    // path is relative to libroot
#include "lib2/lib2.h"    // path is relative to libroot

Setting for linker

链接器的设置

If using static libraries (i.e. .lib file), you will also need to add the library to the linker input, so that at linkage time the symbols can be linked against (otherwise you'll get an unresolved symbol):

如果使用静态库(即 .lib 文件),您还需要将库添加到链接器输入,以便在链接时可以链接符号(否则您将获得未解析的符号):

  1. Right-click on the project, and select Properties.
  2. Select Configuration Properties->Linker->Input
  3. Enter the library under Additional Dependencies.
  1. 右键单击该项目,然后选择“属性”。
  2. 选择配置属性->链接器->输入
  3. 在附加依赖项下输入库。

回答by Assaf Lavie

#includehas nothing to do with projects - it just tells the preprocessor "put the contents of the header file here". If you give it a path that points to the correct location (can be a relative path, like ../your_file.h) it will be included correctly.

#include与项目无关 - 它只是告诉预处理器“将头文件的内容放在这里”。如果你给它一个指向正确位置的路径(可以是一个相对路径,比如 ../your_file.h),它将被正确包含。

You will, however, have to learn about libraries (static/dynamic libraries) in order to make such projects link properly - but that's another question.

但是,您必须了解库(静态/动态库)才能使此类项目正确链接 - 但这是另一个问题。

回答by Benav

Since both projects are under the same solution, there's a simpler way for the include files and linker as described in https://docs.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019:

由于两个项目都在同一个解决方案下,因此包含文件和链接器的方法更简单,如https://docs.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-项目?view=vs-2019

  1. The include can be written in a relative path (E.g. #include "../libProject/libHeader.h").
  2. For the linker, right click on "References", Click on Add Reference, and choose the other project.
  1. 包含可以写在相对路径中(例如#include "../libProject/libHeader.h")。
  2. 对于链接器,右键单击“引用”,单击“添加引用”,然后选择其他项目。

回答by Rosstified

You need to set the path to the headers in the project properties so the compiler looks there when trying to find the header file(s). I can't remember the exact location, but look though the Project properties and you should see it.

您需要在项目属性中设置头文件的路径,以便编译器在尝试查找头文件时查找头文件。我不记得确切的位置,但是查看项目属性,您应该会看到它。

回答by cdonner

Try to avoid complete path references in the #include directive, whether they are absolute or relative. Instead, add the location of the other project's include folder in your project settings. Use only subfolders in path references when necessary. That way, it is easier to move things around without having to update your code.

尽量避免#include 指令中的完整路径引用,无论它们是绝对的还是相对的。相反,在您的项目设置中添加其他项目的包含文件夹的位置。必要时仅使用路径引用中的子文件夹。这样,无需更新代码就可以更轻松地移动内容。

回答by yoyo

Expanding on @Benav's answer, my preferred approach is to:

扩展@Benav 的回答,我的首选方法是:

  1. Add the solution directory to your include paths:
    • right click on your project in the Solution Explorer
    • select Properties
    • select All Configurations and All Platforms from the drop-downs
    • select C/C++ > General
    • add $(SolutionDir)to the Additional Include Directories
  2. Add references to each project you want to use:
    • right click on your project's References in the Solution Explorer
    • select Add Reference...
    • select the project(s) you want to refer to
  1. 将解决方案目录添加到您的包含路径:
    • 在解决方案资源管理器中右键单击您的项目
    • 选择属性
    • 从下拉列表中选择所有配置和所有平台
    • 选择 C/C++ > 常规
    • 添加$(SolutionDir)到附加包含目录
  2. 添加对要使用的每个项目的引用:
    • 在解决方案资源管理器中右键单击您的项目的参考
    • 选择添加引用...
    • 选择您要参考的项目

Now you can include headers from your referenced projects like so:

现在,您可以包含引用项目中的标题,如下所示:

#include "OtherProject/Header.h"

Notes:

笔记:

  • This assumes that your solution file is stored one folder up from each of your projects, which is the default organization when creating projects with Visual Studio.
  • You could now include anyfile from a path relative to the solution folder, which may not be desirable but for the simplicity of the approach I'm ok with this.
  • Step 2 isn't necessary for #includes, but it sets the correct build dependencies, which you probably want.
  • 这假定您的解决方案文件存储在每个项目的一个文件夹中,这是使用 Visual Studio 创建项目时的默认组织。
  • 您现在可以包含相对于解决方案文件夹的路径中的任何文件,这可能是不可取的,但为了方法的简单性,我可以接受。
  • #includes不需要第 2 步,但它设置了您可能需要的正确构建依赖项。