如何减少编译时间:在包含未修改的头文件的情况下
how to reduce compile time: in case of including an untouched header file
我在 file1.h
中定义了一个函数,如下所示:
/** file1.h **/
def testFunc(){}
file1.h是一次性编写的,开发过程中不会修改。但是我必须在我的代码中使用 testFunc()
在 file2
.
中
/** file2.h **/
#include "file1.h"
testFunc(); //some sort of use
问题是每次我对 file2.h
进行更改时,编译器显然也会编译 file1.h
,这需要时间。关于如何阻止编译器每次编译文件的任何建议?
您可以使用预编译 headers 作为解决方案。你如何做到这一点取决于你的编译器和环境。 Here 是 GCC 的开始:
To create a precompiled header file, simply compile it as you would
any other file, if necessary using the -x option to make the driver
treat it as a C or C++ header file. You may want to use a tool like
make
to keep the precompiled header up-to-date when the headers it
contains change.
A precompiled header file is searched for when #include
is seen in the
compilation. As it searches for the included file (see Search Path in
The C Preprocessor) the compiler looks for a precompiled header in
each directory just before it looks for the include file in that
directory. The name searched for is the name specified in the #include
with ‘.gch’ appended. If the precompiled header file cannot be used,
it is ignored.
我在 file1.h
中定义了一个函数,如下所示:
/** file1.h **/
def testFunc(){}
file1.h是一次性编写的,开发过程中不会修改。但是我必须在我的代码中使用 testFunc()
在 file2
.
/** file2.h **/
#include "file1.h"
testFunc(); //some sort of use
问题是每次我对 file2.h
进行更改时,编译器显然也会编译 file1.h
,这需要时间。关于如何阻止编译器每次编译文件的任何建议?
您可以使用预编译 headers 作为解决方案。你如何做到这一点取决于你的编译器和环境。 Here 是 GCC 的开始:
To create a precompiled header file, simply compile it as you would any other file, if necessary using the -x option to make the driver treat it as a C or C++ header file. You may want to use a tool like
make
to keep the precompiled header up-to-date when the headers it contains change.A precompiled header file is searched for when
#include
is seen in the compilation. As it searches for the included file (see Search Path in The C Preprocessor) the compiler looks for a precompiled header in each directory just before it looks for the include file in that directory. The name searched for is the name specified in the#include
with ‘.gch’ appended. If the precompiled header file cannot be used, it is ignored.