使用 GCC 预编译头文件 [英] Precompiled headers with GCC

查看:52
本文介绍了使用 GCC 预编译头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功地使用 GCC 获得预编译头文件?我的尝试没有运气,我没有看到很多关于如何设置它的好例子.我已经尝试过 cygwin gcc 3.4.4 并在 Ubuntu 上使用 4.0.

解决方案

我确实取得了成功.首先,我使用了以下代码:

<代码>#include #include 使用命名空间标准;使用命名空间 boost::xpressive;//一个简单的正则测试int main(){std::string hello( "hello world!" );sregex rex = sregex::compile( "(\w+) (\w+)!" );匹配什么;如果(regex_match(你好,什么,雷克斯)){std::cout <<什么[0]<<'
';//整个匹配std::cout <<什么[1] <<'
';//第一次捕获std::cout <<什么[2] <<'
';//第二次捕获}返回0;}

这只是来自 Boost Xpressive 的一个 hello world(链接见下文).首先,我使用 gcc 中的 -H 选项进行编译.它显示了它使用的大量标题列表.然后,我查看了我的 IDE (code::blocks) 生成的编译标志,看到了如下内容:

g++ -Wall -fexceptions -g -c main.cpp -o obj/Debug/main.o

所以我写了一个命令来编译具有完全相同标志的 Xpressive.hpp 文件:

sudo g++ -Wall -fexceptions -g/usr/local/include/boost/xpressive/xpressive.hpp

我用 -H 再次编译了原始代码并得到了这个输出:

<前>g++ -Wall -fexceptions -H -g -c main.cpp -o obj/Debug/main.o!/usr/local/include/boost/xpressive/xpressive.hpp.gch主程序./usr/include/c++/4.4/iostream../usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h../usr/include/c++/4.4/ostream../usr/include/c++/4.4/istream主程序

!意味着编译器能够使用预编译的头文件.x 表示它无法使用它.使用适当的编译器标志至关重要.我取下 -H 并进行了一些速度测试.预编译的头文件从 14 秒改进到 11 秒.不错,但也不错.

注意:这是示例的链接:http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples 我无法让它在发布.

顺便说一句:我正在使用以下 g++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

Anyone had any success getting precompiled headers working with GCC? I have had no luck in my attempts and I haven't seen many good examples for how to set it up. I've tried on cygwin gcc 3.4.4 and using 4.0 on Ubuntu.

解决方案

I have definitely had success. First, I used the following code:


#include <boost/xpressive/xpressive.hpp>
#include <iostream>

using namespace std;
using namespace boost::xpressive;

//A simple regex test
int main()
{
    std::string hello( "hello world!" );

    sregex rex = sregex::compile( "(\w+) (\w+)!" );
    smatch what;

    if( regex_match( hello, what, rex ) )
    {
        std::cout << what[0] << '
'; // whole match
        std::cout << what[1] << '
'; // first capture
        std::cout << what[2] << '
'; // second capture
    }
    return 0;
}

This was just a hello world from Boost Xpressive (see below for link). First, I compiled with the -H option in gcc. It showed an enormous list of headers that it used. Then, I took a look at the compile flags my IDE (code::blocks) was producing and saw something like this:

g++ -Wall -fexceptions -g -c main.cpp -o obj/Debug/main.o

So I wrote a command to compile the Xpressive.hpp file with the exact same flags:

sudo g++ -Wall -fexceptions -g /usr/local/include/boost/xpressive/xpressive.hpp

I compiled the original code again with the -H and got this output:

g++ -Wall -fexceptions -H  -g     -c main.cpp -o obj/Debug/main.o
! /usr/local/include/boost/xpressive/xpressive.hpp.gch
main.cpp
. /usr/include/c++/4.4/iostream
.. /usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h
.. /usr/include/c++/4.4/ostream
.. /usr/include/c++/4.4/istream
main.cpp

The ! means that the compiler was able to use the precompiled header. An x means it was not able to use it. Using the appropriate compiler flags is crucial. I took off the -H and ran some speed tests. The precompiled header had an improvement from 14 seconds to 11 seconds. Not bad but not great.

Note: Here's the link to the example: http://www.boost.org/doc/libs/1_43_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.examples I couldn't get it to work in the post.

BTW: I'm using the following g++

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

这篇关于使用 GCC 预编译头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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