简体   繁体   English

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

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

I am working on a game using Visual C++.我正在使用 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?如何#include 来自不同项目的头文件? I have no idea how to use classes from one project in another.我不知道如何在另一个项目中使用一个项目中的类。

Settings for compiler编译器设置

In the project where you want to #include the header file from another project, you will need to add the path of the header file into the Additional Include Directories section 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.选择配置属性->C/C++->常规。
  3. Set the path under Additional Include Directories.在附加包含目录下设置路径。

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 (ie .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.在附加依赖项下输入库。

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 (Eg #include "../libProject/libHeader.h" ).包含可以写在相对路径中(例如#include "../libProject/libHeader.h" )。
  2. For the linker, right click on "References", Click on Add Reference, and choose the other project.对于链接器,右键单击“引用”,单击“添加引用”,然后选择其他项目。

#include has nothing to do with projects - it just tells the preprocessor "put the contents of the header file here". #include与项目无关 - 它只是告诉预处理器“将头文件的内容放在这里”。 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.如果你给它一个指向正确位置的路径(可以是一个相对路径,比如 ../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.但是,您必须了解库(静态/动态库)才能使此类项目正确链接 - 但这是另一个问题。

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选择 C/C++ > 常规
    • add $(SolutionDir) to the Additional Include Directories$(SolutionDir)添加到附加包含目录
  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选择您要参考的项目

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.这假定您的解决方案文件存储在每个项目的一个文件夹中,这是使用 Visual Studio 创建项目时的默认组织。
  • You could now include any file 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 #include s, but it sets the correct build dependencies, which you probably want. #include不需要第 2 步,但它设置了您可能需要的正确构建依赖项。

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.我不记得确切的位置,但是查看项目属性,您应该会看到它。

Try to avoid complete path references in the #include directive, whether they are absolute or relative.尽量避免#include 指令中的完整路径引用,无论它们是绝对的还是相对的。 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.这样,无需更新代码就可以更轻松地移动内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GoogleTest在同一解决方案中几个C ++项目 - GoogleTest several C++ projects in the same solution 将DLL与C ++项目中的其他输出文件分开 - Separate dll from other output files in c++ projects 如何在同一个解决方案中包含其他项目的头文件? - How to include header files of another projects in the same solution? 从 Visual Studio 解决方案和项目(不是 cmake)中的非托管 (C++/C) 代码创建 Nuget 包 - Create Nuget package from unmanaged (C++/C) code in Visual Studio Solution and projects (not cmake) Visual Studio C++ 不能在同一解决方案或外部文件夹中包含其他目录 - Visual Studio C++ Can't include additional directory within same solution or outside folder c ++ visual studio 2008问题,在一个解决方案中有两个项目 - c++ visual studio 2008 issue with two projects in one solution 通过脚本将项目添加到visual studio c ++解决方案 - Adding projects to a visual studio c++ solution via a script visual studio c++ 解决方案中有两个独立项目的问题? - visual studio c++ problem with two independent projects in a solution? Visual Studio 2010 C ++可以继承包含引用项目的路径吗? - Can Visual Studio 2010 C++ inherit include path from referenced projects? 在同一解决方案 C++ express 上的许多项目中更改平台工具集 - Change Platform toolset in many projects on the same solution C++ express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM