gcc precompiled头怪异的行为与-c选项 [英] gcc precompiled headers weird behaviour with -c option
问题描述
短小故事:
我无法使预编译的标头使用gcc -c选项正常工作。
I can't make precompiled headers work properly with gcc -c option.
长故事:
我在Linux上使用gcc-4.4.1,在一个非常大的项目中尝试预编译头之前,我决定在简单程序。他们有点工作,但我不满意结果,我确定我的设置有问题。
Folks, I'm using gcc-4.4.1 on Linux and before trying precompiled headers in a really large project I decided to test them on simple program. They "kinda work" but I'm not happy with results and I'm sure there is something wrong about my setup.
首先,我写了一个简单的程序(main.cpp)来测试它们是否工作:
First of all, I wrote a simple program(main.cpp) to test if they work at all:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/type_traits.hpp>
int main()
{
return 0;
}
$ b <如下:
Then I created the precompiled headers file pre.h(in the same directory) as follows:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/type_traits.hpp>
...并编译:
$ g++ -I. pre.h
(pre.h.gch已创建)
(pre.h.gch was created)
之后,我测量了有无预编译头文件的编译时间:
After that I measured compile time with and without precompiled headers:
与pch
$ time g++ -I. -include pre.h main.cpp
real 0m0.128s
user 0m0.088s
sys 0m0.048s
不含pch
$ time g++ -I. main.cpp
real 0m0.838s
user 0m0.784s
sys 0m0.056s
到目前为止这么好! 速度快了7倍,令人印象深刻!现在让我们尝试一些更现实的东西。所有我的源是用-c选项构建的,由于某种原因,我不能让pch玩得很好。
So far so good! Almost 7 times faster, that's impressive! Now let's try something more realistic. All my sources are built with -c option and for some reason I can't make pch play nicely with it. You can reproduce this with the following steps below...
我创建了测试模块foo.cpp,如下所示:
I created the test module foo.cpp as follows:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/type_traits.hpp>
int whatever()
{
return 0;
}
这里是我尝试构建模块foo.cpp的时间,无pch:
Here are the timings of my attempts to build the module foo.cpp with and without pch:
with pch
$ time g++ -I. -include pre.h -c foo.cpp
real 0m0.357s
user 0m0.348s
sys 0m0.012s
不含pch
$ time g++ -I. -c foo.cpp
real 0m0.330s
user 0m0.292s
sys 0m0.044s
这很奇怪,看起来没有加速了!(我跑了几次计时)。结果是预编译头在这种情况下根本没有使用,我检查了-H选项(g ++ -I。输出-include pre.h -c foo.cpp -H没有列出pre.h.
That's quite strange, looks like there is no speed up at all!(I ran timings for several times). It turned out precompiled headers were not used at all in this case, I checked it with -H option(output of "g++ -I. -include pre.h -c foo.cpp -H" didn't list pre.h.gch at all).
推荐答案
> Ok,我想我找到了解决方案: -fpch-preprocess 应与 -c 选项一起使用。
Ok, I think I've found the solution: -fpch-preprocess should be used alongside with -c option. It works like a charm!
/ p>
with pch
$ time g++ -I. -include pre.h -c foo.cpp -fpch-preprocess
real 0m0.028s
user 0m0.016s
sys 0m0.016s
without pch
$ time g++ -I. -c foo.cpp
real 0m0.330s
user 0m0.292s
sys 0m0.044s
更新:我在gcc帮助邮件列表上提出了同样的问题,Ian Lance Taylor用我的distcc / ccache解释了这个奇怪的行为。这些工具首先预处理源,这就是为什么这个选项是必需的。
Update: I asked the same question on the gcc help mailing list and Ian Lance Taylor explained this strange behavior by my usage of distcc/ccache. These tools first preprocess the source that's why this options is required.
这篇关于gcc precompiled头怪异的行为与-c选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!