gpt4 book ai didi

c++ - 在每个源文件中包含一个头文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:45 25 4
gpt4 key购买 nike

假设您有 100 个源文件(.c 或 .cpp)文件,并且您希望在每个文件中包含一些定义、函数/变量声明。通常在 C/C++ 中,您为此目的使用头文件。但在这种情况下,您需要在每个源文件中放入#include "header.h"。

现在我的问题是,有没有一种方法可以为所有文件包含一个 header ,而无需在每个文件中都添加#include "header.h",因为编写#include "header.h"会很烦人数百个源文件。

最佳答案

您可以为 clang 或 GCC 使用 -include 标志。来自man page :

-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.

例子:

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 flag ,这很相似。

关于c++ - 在每个源文件中包含一个头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com