编译时间慢,使用 Boost + GCC + 预编译标头

Slow Compile Time with Boost + GCC + precompiled header

本文关键字:编译 GCC Boost 时间 使用      更新时间:2023-10-16

正在运行:gcc 版本 4.2.1(Apple Inc. build 5664)

我创建了一个带有默认预编译标头的 apple XCode 项目。 它看起来很慢,一个具有 main 函数 no 包含没有代码的琐碎主文件需要 6 秒才能编译,这是在我升级到新的 SSD 驱动器之后。 我在笔记本电脑上,但我对升级到工作站可以缓解我的问题有所保留。 如果我关闭预编译标头,则主文件将在一秒钟内编译。 使用预编译标头似乎会对所有文件造成惩罚。 这种延迟使我想避免编译和试验不好的代码。 这是我在预编译标头中包含的内容:

#pragma once
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <valarray>
#include <vector>
#include <boost/smart_ptr/scoped_ptr.hpp>
#include <boost/smart_ptr/scoped_array.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/shared_array.hpp>
#include <boost/smart_ptr/make_shared.hpp>  
#include <boost/smart_ptr/weak_ptr.hpp>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/regex.hpp>
#include <boost/thread.hpp>
#include <boost/bind/bind.hpp>
#include <boost/bind/apply.hpp>
#include <boost/bind/protect.hpp>
#include <boost/bind/make_adaptable.hpp>
#include <boost/asio.hpp>
//#include <boost/asio/ssl.hpp>

#include <boost/property_tree/ptree.hpp>
#include <boost/random.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/time_zone_base.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>

我没有包括精神,这确实使编译时间增加了。

GCC的预编译标头以非常特殊的方式工作。在任何给定的源文件中只能使用一个预编译头文件。使用 -H 显示给定头文件是否正在使用预编译版本。

此外,必须使用与使用它的源文件完全相同的编译器标志编译头文件。

设置 PCH 环境的典型方法是这样的:

主.cpp:

#include "allheaders.hpp"
int main() { /* ... */ }

Allheaders.hpp:

#include <algorithm>
// ... everything you need

汇编:

g++ $CXXFLAGS allheaders.hpp                 # 1
g++ $CXXFLAGS -H -c -o main.o main.cpp       # 2
g++ $LDFLAGS -o myprogram main.o             # 3

在步骤#1之后,您应该最终得到一个文件allheaders.hpp.gch,这应该非常大。在步骤 #2 中,-H 标志应生成其他输出,告诉您正在使用预编译头文件。步骤 #3 链接可执行文件。

这个想法是步骤#1可能需要很长时间,但步骤#2应该变得更快。