简体   繁体   English

Eclipse CDT - 预编译头

[英]Eclipse CDT - Precompiled Header

I am looking for a straight forward way to use precompiled headers for a C++ project using Eclipse / CDT. 我正在寻找一种直接的方法来使用Eclipse / CDT为C ++项目使用预编译头。 The stuff does work when running from command line but I am looking for an easy way to integrate it into Eclipse. 从命令行运行时,这些东西确实有效,但我正在寻找一种将它集成到Eclipse中的简单方法。

Any suggestions ?! 有什么建议么 ?!

From what i've seen, currently Eclipse CDT doesn't support precompiled headers directly. 从我所看到的,目前Eclipse CDT不直接支持预编译头。 I mean, you can't set an option like : "For this header, compile it". 我的意思是,你不能设置如下选项:“对于这个标题,编译它”。 The same applies for headers of external libraries of your eclipse project. 这同样适用于eclipse项目的外部库的头文件。

As you know, you need to set the same compilers flag for the header compilation in order to get the compiler use it for the other compilation unit. 如您所知,您需要为标头编译设置相同的编译器标志,以便让编译器将其用于其他编译单元。 (At least in the case of GCC). (至少在海湾合作委员会的情况下)。

So, you have several solutions : 那么,你有几个解决方案:

  • You replace the default build command found in "Properties for -> C/C++ Build -> Builder Settings -> Build command" with a custom script that will modify the makefiles generated by eclipse and then call make. 您可以使用自定义脚本替换“属性 - > C / C ++构建 - >构建器设置 - >构建命令”中的默认构建命令,该脚本将修改eclipse生成的makefile,然后调用make。 A ruby solution is proposed here . 这里提出一种红宝石溶液。 Note, that it doesn't allow the use of multiple precompiled headers. 请注意,它不允许使用多个预编译头。
  • You can use ccache instead of using GCC. 您可以使用ccache而不是使用GCC。 It detects when the same compilation is done again, so it's a bit different from using precompiled headers. 它会检测何时再次进行相同的编译,因此它与使用预编译的头有点不同。

EDIT : 编辑:

To get the ruby script working, you need to have at least one .cpp file inside the same directory as your header file. 要使ruby脚本正常工作,您需要在头文件所在的目录中至少有一个.cpp文件。 Otherwise you'll get an error on a missing file "subdir.mk". 否则,您将在丢失的文件“subdir.mk”上收到错误消息。

Another approach is to create a source file (eg pch.cpp) with build settings changed to create the precompiled header. 另一种方法是创建一个源文件(例如pch.cpp),更改构建设置以创建预编译头。 It should be included in the build in order to create the precompiled header, and then excluded in order to build the final executable. 它应该包含在构建中,以便创建预编译的头,然后排除以构建最终的可执行文件。 It needs to be temporarily reincluded if the header file is changed. 如果更改头文件,则需要暂时​​重新包含它。

This website provides more details, including a way of creating different precompiled headers for each build configuration. 该网站提供了更多详细信息,包括为每个构建配置创建不同预编译头的方法。

Just to help others who may stumble upon this (since i spent some time figuring this out) 只是为了帮助那些可能偶然发现这个问题的人(因为我花了一些时间来搞清楚这一点)

The first thing to do is create a PCH folder (if you want) with a pch.cpp and pch.h file. 首先要做的是使用pch.cpp和pch.h文件创建一个PCH文件夹(如果需要)。

pch.cpp: (this file is to compile the .gch) pch.cpp :(这个文件是为了编译.gch)

#include "pch.h"

pch.h: pch.h:

#include <iostream>
#include <string>
// more stuff that's not changing anytime soon...

The second thing to do is create a PCH build configuration. 第二件事是创建PCH构建配置。

exclude all your project source files from this build. 从此版本中排除所有项目源文件。 (we only need to compile pch.cpp) (我们只需要编译pch.cpp)
select the files/folders > Right Click > Resource Configurations > Exclude from Build > PCH 选择文件/文件夹>右键单击>资源配置>从构建中排除> PCH

Now we need to make it compile as a .gch (precompiled header) 现在我们需要将其编译为.gch(预编译头)
pch.cpp > Right Click > Properties > C/C++ Build > Settings > Tool Settings pch.cpp>右键单击>属性> C / C ++生成>设置>工具设置
Compiler > Pattern: ${COMMAND} ${FLAGS} ${INPUTS} (remove all the output stuff) 编译器>模式: ${COMMAND} ${FLAGS} ${INPUTS} (删除所有输出内容)
Misc > Flags: -c -x c++-header -o "../src/PCH/pch.h.gch" 其他>标志: -c -x c++-header -o "../src/PCH/pch.h.gch"

Now when you build with this configuration, it'll produce the pch.h.gch where pch.h is. 现在,当您使用此配置构建时,它将生成pch.h所在的pch.h.gch。
(it'll also give a meaningless error trying to create an exe. just ignore that) (它也会在尝试创建exe时出现无意义的错误。只需忽略它)


Back on the normal Release build, exclude pch.cpp since you don't need to compile that. 回到正常的发布版本,排除pch.cpp,因为您不需要编译它。

Now it's time to test the .gch is being used over the .h 现在是时候测试.gch正在使用.h

at the top of pch.h put #error "not using precompiled header file" 在pch.h的顶部放#error "not using precompiled header file"

The .ghc is only used if the header is included first, in the compiling file. 只有在编译文件中首先包含标题时才使用.ghc。

"Only one precompiled header can be used in a particular compilation." “在特定编译中只能使用一个预编译头。”

you should precompile different selections of headers best fit for different compilation units 您应该预编译最适合不同编译单元的不同标题选择


COMPILE TESTS 编译测试

without precompiled headers 没有预编译的头文件
took 34s.189ms output src=2,143,078 bytes exe=1,346,864 bytes

with -include ../src/PCH/pch.h (included in every compilation unit) (only the pch.h not the .gch) with -include ../src/PCH/pch.h(包含在每个编译单元中)(只有pch.h而不是.gch)
took 48s.364ms output src=2,159,431 bytes exe=1,355,298 bytes
(this is why we #include manually where needed so it should still take around 34s to compile without .gch) (这就是为什么我们在需要的地方手动#include所以它仍然需要大约34秒来编译而没有.gch)

with -include ../src/PCH/pch.h and pch.h.gch (fast compilation, but still inefficient with -include) with -include ../src/PCH/pch.h和pch.h.gch(快速编译,但使用-include仍然效率低下)
took 22s.535ms output src=2,159,431 bytes exe=1,355,298 bytes

if the pch's are organized/utilized properly (correct choice of headers and files to include them in) (not used in every file) it should be much faster than 22s. 如果正确组织/使用pch(正确选择标题和文件以包含它们)(不在每个文件中使用)它应该比22s快得多。 (this test was just done on a project which weren't built with pch's in mind) (这个测试刚刚在一个没有用pch构建的项目上完成)


additional speedup which can triple compile time 额外的加速,可以使编译时间增加三倍
Project > Properties > C/C++ Build > Behavior > Enable parallel build 项目>属性> C / C ++构建>行为>启用并行构建

考虑使用cmakecotire

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM