[英]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.
我不知道如何在另一个项目中使用一个项目中的类。
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:要访问项目配置:
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
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 文件),您还需要将库添加到链接器输入,以便在链接时可以链接符号(否则您将获得未解析的符号):
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 :
#include "../libProject/libHeader.h"
).#include "../libProject/libHeader.h"
)。#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 的回答,我的首选方法是:
$(SolutionDir)
to the Additional Include Directories$(SolutionDir)
添加到附加包含目录Now you can include headers from your referenced projects like so:现在,您可以包含引用项目中的标题,如下所示:
#include "OtherProject/Header.h"
Notes:笔记:
#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.