gpt4 book ai didi

c++ - 使用 Boost + GCC + 预编译 header 的编译时间变慢

转载 作者:可可西里 更新时间:2023-11-01 18:29:41 27 4
gpt4 key购买 nike

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

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

#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>

我没有包含 spirit ,这确实使编译时间上升。

最佳答案

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

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

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

ma​​in.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 步应该变得更快。

关于c++ - 使用 Boost + GCC + 预编译 header 的编译时间变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com