C++ 使用命令行选项包含头文件?

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

Include header files using command line option?

c++gccinclude

提问by jdm

Is it possible to specify extra header files to include from the command line (using GCC 4 / C++)?

是否可以从命令行指定额外的头文件(使用 GCC 4 / C++)?

Or is there any other way files can be included except with #include?

或者除了#include 之外,还有其他方法可以包含文件吗?

Background: I'm trying to compile a large code base on my own PC. The code is usually compiled in a cluster, with a complicated build system (SoftRelTools anybody?), which is intertwined with the operating system, such that it is virtually impossible to install it somewhere else (literally hundreds of makefiles and shell scripts, and hard coded paths to network drives). However, the actual code is fairly straightforward, and compiles fine, BUT it is missing a lot of includes (mostly a la "include <vector>" and "include <math.h>"). I'm guessing the build system takes care of this usually, but I have to go through the code and add the includes manually, which I'd rather avoid.

背景:我正在尝试在自己的 PC 上编译大型代码库。代码通常是在一个集群中编译的,有一个复杂的构建系统(SoftRelTools 任何人?),它与操作系统交织在一起,因此几乎不可能将它安装在其他地方(字面意思是数百个 makefile 和 shell 脚本,并且很难到网络驱动器的编码路径)。然而,实际代码相当简单,并且编译得很好,但它缺少很多包含(主要是“ include <vector>”和“ include <math.h>”)。我猜构建系统通常会处理这个问题,但我必须通过代码并手动添加包含,我宁愿避免这种情况。

回答by foraidt

I foundthe -include option. Does this what you want?

找到了 -include 选项。这是你想要的吗?

-include file

Process file as if "#include "file"" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the "#include "..."" search chain as normal.

If multiple -include options are given, the files are included in the order they appear on the command line.

-包含文件

处理文件就像“#include“file””作为主要源文件的第一行出现。但是,搜索文件的第一个目录是预处理器的工作目录,而不是包含主源文件的目录。如果在那里没有找到,它会像往常一样在 "#include "..."" 搜索链的其余部分中搜索。

如果给出了多个 -include 选项,则文件将按照它们在命令行中出现的顺序包含在内。

回答by YuppieNetworking

From the gcc manual:

从 gcc 手册:

-include file

Process file as if "#include "file"" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the "#include "..."" search chain as normal.

If multiple -include options are given, the files are included in the order they appear on the command line.

-包含文件

处理文件就像“ #include "file"”出现在主要源文件的第一行一样。但是,搜索文件的第一个目录是预处理器的工作目录,而不是包含主源文件的目录。如果在那里没有找到,它会#include "..."像往常一样在 " " 搜索链的其余部分中搜索。

如果给出了多个 -include 选项,则文件将按照它们在命令行中出现的顺序包含在内。

回答by tibur

According to gcc documentation, the command line switch "-include file" would do the job.

根据 gcc 文档,命令行开关“-include file”可以完成这项工作。