如何使用代码块和 gcc 创建预编译头
我有一个已经过测试并且按预期工作的文件:
#ifndef PROMOTE_H_INCLUDED
#define PROMOTE_H_INCLUDED
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/deref.hpp>
template<class Integral>
struct Promote
{
typedef boost::mpl::vector<char,short,int,long,long long> types;
typedef typename boost::mpl::find<types,Integral>::type this_type;
typedef typename boost:: mpl::next<this_type>::type next_type;
typedef typename boost::mpl::deref<next_type>::type type;
};
#endif // PROMOTE_H_INCLUDED
每次我在项目中更改某些内容时,这个文件都会被一遍又一遍地编译,这有点愚蠢。我尝试搜索网络,发现:
http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html 但说实话,我只是没有在任何地方看到如何创建预编译头的说明。那么有人可以一步步告诉我如何使用 code::blocks 来做到这一点吗?
谢谢。
I have a file which has been tested and works as intended:
#ifndef PROMOTE_H_INCLUDED
#define PROMOTE_H_INCLUDED
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/deref.hpp>
template<class Integral>
struct Promote
{
typedef boost::mpl::vector<char,short,int,long,long long> types;
typedef typename boost::mpl::find<types,Integral>::type this_type;
typedef typename boost:: mpl::next<this_type>::type next_type;
typedef typename boost::mpl::deref<next_type>::type type;
};
#endif // PROMOTE_H_INCLUDED
Every time I change something in my project this file is being compiled over and over again which is bit sill. I tried to search net and I found:
http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html
But to be honest I just don't see anywhere there instruction how to create precompiled header. So could anyone, step by step tell me how to do it using code::blocks?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您链接的文档中:
因此:
对于 C++ 代码将创建预编译头。
但它不会以您似乎想要的方式加快构建过程。如果更改该标头,您还需要更新预编译标头及其所有依赖项。
From the docs you link:
So:
for C++ code will create the precompiled header.
It wont speed up the build process in the way you seem to want it though. If you change that header, you'll need to update the precompiled header too, and all its dependencies.
编译 .h 文件时不需要 -x 和 -c 选项。 (通常也不需要 -o 选项,除非标头名称与所需的预编译标头名称不对应。)请注意,gcc 文档声明使用 -x 选项使编译器处理输入文件作为头文件 - 但 - 仅“如果需要”。对于 .h 文件,不需要 -x。g++ 知道如何处理 .h 文件作为输入文件。因为 .h 文件是头文件, g++ 不会编译它到一个对象,所以不需要 -c 和 -o 。 g++ 允许编译 .h 的全部目的(据我所知)确实是创建一个预编译头(.h.gch),
只需使用
g++ pch 。 .h
创建 pch.h.gch(我已使用 gcc 10.2 对此进行了测试)GCC 预编译也可以使用 .cpp,在这种情况下,您可以使用-x 选项使 .cpp 文件被编译为头文件。
g++ -x c++-header -c pch.cpp -o pch.h.gch
(我已经使用 gcc 10.2 对此进行了测试)
对于包含 pch.h 的 cpp 文件,可以使用 -H 编译选项来确保正确使用预编译。例如
g++ -H 示例SrcFile.cpp
其中#include "pch.h" 是ExampleSrcFile.cpp 中的第一个语句。
The -x and -c options are not needed when compiling a .h file. (The -o option is also generally not needed, unless for example, the header name doesn't correspond to the desired precompiled header name.) Note that the gcc docs state to use the -x option to cause the compiler to treat the input file as a header file - but - only 'if needed". For a .h file, the -x is not needed. g++ knows what to do with a .h file as input file. Because a .h file is a header file, g++ will not compile it to an object, so -c and -o are not needed. The entire purpose (AFAIK) of g++ allowing compiling of a .h, is indeed to create a precompiled header (.h.gch) .
Simply use
g++ pch.h
to create pch.h.gch. (I have tested this with gcc 10.2) GCC PrecompileA .cpp may also be used, in which case you do use the -x option to cause the .cpp file to be compiled as a header file. E.g.
g++ -x c++-header -c pch.cpp -o pch.h.gch
(I have tested this with gcc 10.2)
With cpp files that include pch.h, the -H compile option can be used to ensure that the precompiled is being consumed properly. E.g.
g++ -H ExampleSrcFile.cpp
where #include "pch.h" is the first statement in the ExampleSrcFile.cpp.