在C ++中的include语句中使用快捷方式的(相对)路径 [英] Using (relative) paths to shortcut in include statements in C++

查看:144
本文介绍了在C ++中的include语句中使用快捷方式的(相对)路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我开始使用C ++进行编码.我将Windows用作编写代码的操作系统.我一直将所有写得好的"代码都放在一个位置.现在,我正在一个需要使用这些代码的项目中.因此,我计划将那些我需要的文件作为头文件包含在我的项目中.但是,为了使我的项目独立",我创建了我需要的那些文件夹的快捷方式,并将它们保留在新项目的源文件夹中,并决定在其中的"include"语句中使用快捷方式的相对路径.我的项目.

I started coding in C++ few days ago. I am using windows as my OS for writing code. I have been keeping all of my "well-written" codes in a single location. Now, I am working on a project that requires the use of those codes. So, I planned to include those files that I require as header files in my project. However, to make my project "self-contained", I created shortcuts of those folders that I require and kept them in the source folder of my new project and decided to use relative paths to the shortcuts in the "include" statements in my project.

但是,我遇到一个错误.在Windows中的C ++的include语句中,有没有办法使用相对(或通常是绝对)路径的快捷方式?

However, I am getting an error. Is there any way to use relative (or, in general, absolute) paths to shortcuts in the include statements of C++ in windows?

谢谢.

推荐答案

这实际上取决于如何包含头文件.

It really depends on how you include the header files.

如果您使用双引号括起来,例如

If you include with double-quotes, like e.g.

#include "some_header_file.h"

然后相对路径从当前文件位置开始.

Then the relative path is from the current files location.

如果您包括使用尖括号,例如

If you include using angle-brackets, like e.g.

#include <some_header_file.h>

然后相对路径基于系统包含路径.

Then the relative path is based on the system include paths.

您始终可以向系统包含路径添加路径.如何执行取决于您的环境和编译器.如果使用的是Visual Studio,则进入项目属性对话框,然后在"C/C ++"/常规"选项卡中有一个名为其他包含目录"的字段,您可以在其中添加目录. (这是针对VS 2015的,在其他版本上可能会有所不同.)

You can always add a path to the system include path. How to do it depend on your environment and compiler. If you're using Visual Studio you go into the project properties dialog, and in the "C/C++" / "General" tab there is a field called "Additional Include Directories" where you can add directories. (This is for VS 2015, might be a little different on other versions.)

关于双引号包含.可以说您的项目层次结构如下所示(在 disk 上!):

Regarding double quotes inclusion. Lets say your project hierarchy looks like this (on disk!):


Project
|-- Include
|-- Source
|   `-- MoreSource
`-- Other

Project/Source中,您有您的源文件,并且如果其中一个要包含Project/Include中的头文件,则其外观类似于

In Project/Source you have your source files, and if one of them want to include a header file from Project/Include, then it will look something like

#include "../Include/header.h"

现在,如果Project/Source/MoreSource中有一个源文件想要包含相同的头文件,则将是

Now if you have a source file in Project/Source/MoreSource that want to include the same header file it will be

#include "../../Include/header.h"

Project/Include目录添加到系统标题搜索路径可能很有用.您仍然可以使用双引号将文件包括在内,因为如果找不到它们,则预处理器也会搜索系统路径,但是您不需要完整"相对路径.如果将Project/Include添加到系统头文件路径,则可以只写

It could be useful to add the Project/Include directory to the system header search path. You can still use double-quotes to include the files, since if they are not found then the preprocessor will search the system paths as well, but you don't need the "full" relative path. If you add Project/Include to the system header path, you could write just

#include "header.h"

#include <header.h>

但是请注意,如果头文件的名称与实际系统头文件的名称相同,则可能会遇到麻烦.

Be careful though, if you have a header file with the same name as an actual system header file you might have some trouble.

这篇关于在C ++中的include语句中使用快捷方式的(相对)路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆