C++中有跨平台的预编译头框架/方法吗?
我想知道其他人在 C++ 中实现跨平台(linux 和 windows)预编译头的经历。 我正在考虑 Visual Studio 可以让您使用 stdafx.h 文件做什么,这些文件可以通过预编译全面使用的通用标头(std/boost/etc 标头)来显着缩短大量 C++ 代码的编译时间。 有没有办法通过某种框架或其他方式实现跨平台?
您尝试这样做有什么样的经历?
编辑
我并不是真正的意思是共享实际生成的 pch,我更感兴趣的是可以生成 pch 的框架以及在该特定平台上编译时的任何等效项,例如 gcc。
I'm wondering what others have experienced implementing cross-platform (linux and windows) precompiled headers in C++. I'm thinking of what Visual Studio lets you do with stdafx.h files that can drastically improve compile times for large amounts of C++ code by precompiling the common headers used across the board (std/boost/etc headers). Is there a way to make this happen cross platform with some kind of framework or something?
What kind of experience have you had trying to do this?
Edit
I don't really mean sharing the actual resulting pch, I'm more interested in frameworks that can generate pch and whatever the equivelant would be for, say, gcc, when compiled on that specific platform.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您传递头文件而不是实现文件,
gcc
将自动预编译头文件。 如果它尝试生成目标代码,只需添加-x
开关即可。 GCC 将始终在标头之前查找.gch
文件,因此使用起来很简单。 此页面有更多信息。 将其添加到您的Makefile
中以自动化该过程。gcc
will automatically precompile headers if you pass a header file instead of an implementation file. Simply add the-x
switch if it tries to produce object code. GCC will always look for the.gch
file before the header, so it's simple enough to use. This page has more info. Add it to yourMakefile
to automate the process.Visual Studio 预编译头基于一个头文件,其中包括所有应预编译的内容,通常通常包括很少更改的头文件,例如标准库内容。 它连接到一个stdafx.cpp,在设置中设置为“生成预编译头”,它只包含stdafx.h。
然后,Visual Studio 强制所有文件包含 stdafx.h 作为其第一个预处理器定义,以避免其之前包含的标头出现问题或更改影响 stdafx.h 解析的 #define 宏。
我认为将此行为映射到 g++ 的最简单方法是使其仅预编译 stdafx.h 并正常包含其他标头。 它与您在 Visual C++ 中所做的类似。 您还可以将 stdafx 重命名为不太愚蠢的名称,例如“precompiled_.h 或其他名称。设置 Visual Studio 来使用此文件很容易。
我已经使用 g++ 的 make 文件实现了这种系统,它提供了一些性能,但我未能获得与 Visual Studio 中的预编译标头相同的性能提升,这是不久前的事,从那时起,我已经设法让 CMake 生成带有预编译标头的 Visual Studio 项目。 ,我还没有尝试过生成 Makefile,但
Visual Studio 有一些其他技巧可以提高编译速度,其中之一是使用相同的设置批量编译许多 cpp 文件,这可以手动完成。使用通常称为统一构建系统的方式,将多个 cpp 文件包含到一个文件中并一次性构建它,从而节省标头解析和磁盘 io。
The visual studio precompiled headers are based on a header file including all that should be precompiled, typically commonly included rarely changed header files such as standard library stuff. It's connected to a stdafx.cpp which is set to "generate precompiled header" in the settings, it only includes stdafx.h.
Visual studio then forces all files to include stdafx.h as its first preprocessor definition to avoid problems with headers included before it or changed #define macros that affects the parsing of stdafx.h.
I think the easiest way of mapping this behaviour to g++ is to make it precompile only stdafx.h and include other headers normally. It will be similar to what you do in visual c++. You can also rename stdafx to something less stupid like "precompiled_.h or something. It's easy to setup visual studio to use this file instead.
I have implemented this kind of system with using make files for g++ and it gave some performance, but I didn't manage to get the same kind of performance boost as I get from precompiled headers in visual studio. This was some time ago and g++ might have improved since then. I've managed to get CMake to generate visual studio projects with precompiled headers, I haven't tried it for their Makefile generation yet but it should be no problem.
Visual Studio has some other tricks to improve compilation speed. One is compiling many cpp-files with the same settings in one batch. This could be done manually using what's usually called a unity build system where you include multiple cpp-files into one file and build it in one go, saving you header parsing and disk io.
如果您的意思是在平台之间移植预编译头数据库(.pch 或其他),那么这是不可能的,因为头文件在不同平台上将具有不同的内容。
If you mean porting the precompiled header database (.pch or whatever) between platforms, then this is not possible as the headers will have different contents on the different platforms.