包括在每一个源文件的头文件 [英] Include one header file in each source file

查看:117
本文介绍了包括在每一个源文件的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有源文件(.c和的.cpp)文件100年代,你想加入一些定义,函数/变量在他们每个人的声明。通常,在C / C ++,您可以使用头文件用于这一目的。但在这种情况下,你需要把#包括header.h在每一个源文件。

Say you have 100s of source files (.c or .cpp) files, and you want to include some definitions, function/variable declarations in each of them. Normally in C/C++, you use header files for that purpose. But in this case you need to put #include "header.h" in each source file.

现在我的问题是,是否有办法包括不把的#includeheader.h在每一个文件的所有文件的一个头,因为这将是很烦人写的#includeheader.h源文件的100S。

Now my question is, is there a way to include one header for all the files without putting #include "header.h" in each of the file, because it will be very tiresome to write #include "header.h" for 100s of source files.

推荐答案

您可以使用 -include 标志铛或GCC。从手册页

You can use the -include flag for clang or GCC. From the man page:

-include 文件

工艺的文件的仿佛#包括文件,出现作为主源文件的第一行。然而,第一个目录搜索的文件的是preprocessor的工作目录中的而不是的包含主源文件的目录。如果没有找到,搜索它在该#包括...搜索链为正常的其余部分。

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.

如果多个 -include 给出的选项,这些文件都包含在它们出现在命令行上的顺序。

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

例如:

clang -include header.h -c file1.c
clang -include header.h -c file2.c
clang -include header.h -c file3.c
clang -o app file1.o file2.o file3.o

MSVC有 / FI 标志,这是类似的。

MSVC has the /FI flag, which is similar.

这篇关于包括在每一个源文件的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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