在跨平台代码中处理stdafx.h [英] Handling stdafx.h in cross-platform code

查看:1384
本文介绍了在跨平台代码中处理stdafx.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Visual Studio C ++的程序,使用预编译头( stdafx.h )。现在我们使用gcc 4.x将应用程序移植到Linux。

I have a Visual Studio C++ based program that uses pre-compiled headers (stdafx.h). Now we are porting the application to Linux using gcc 4.x.

问题是如何在两个环境中处理预编译的标头。
我已经google了,但不能得出结论。

The question is how to handle pre-compiled header in both environments. I've googled but can not come to a conclusion.

显然,我想在Visual Studio中留下 stdafx.h ,因为代码库相当大, boost编译时间。

Obviously I want leave stdafx.h in Visual Studio since the code base is pretty big and pre-compiled headers boost compilation time.

但问题是在Linux中做什么。这是我发现的:

But the question is what to do in Linux. This is what I found:


  1. 保留 stdafx.h gcc编译代码的速度比VC ++快(或者它只是我的Linux机器更强... :)),所以我可能很高兴这个选项。

  2. 使用方法从< a href =http://gamesfromwithin.com/?p=39>这里 - make stdafx.h 看起来像(set USE_PRECOMPILED_HEADER for VS only):

  1. Leave the stdafx.h as is. gcc compiles code considerable faster than VC++ (or it is just my Linux machine is stronger ... :) ), so I maybe happy with this option.
  2. Use approach from here - make stdafx.h look like (set USE_PRECOMPILED_HEADER for VS only):

#ifdef USE_PRECOMPILED_HEADER
... my stuff
#endif 


  • 使用在这里 - 用 / FI 编译VC ++到 implicitly 在每个cpp文件中包含 stdafx.h 。因此在VS中你的代码可以很容易地被编译没有预编译头,没有代码将被改变。

    我个人不喜欢依赖和混乱 stdafx.h 正在推动一个大代码库。因此,选项是吸引我的 - 在Linux上你没有 stdafx.h ,而仍然能够打开VS上的预编译头由<$ c $

  • Use the approach from here - compile VC++ with /FI to implicitly include stdafx.h in each cpp file. Therefore in VS your code can be switched easily to be compiled without pre-compiled headers and no code will have to be changed.
    I personally dislike dependencies and the mess stdafx.h is pushing a big code base towards. Therefore the option is appealing to me - on Linux you don't have stdafx.h, while still being able to turn on pre-compiled headers on VS by /FI only.

    您的意见?有没有其他方法来处理这个问题?

    Your opinion? Are there other approaches to treat the issue?

    推荐答案

    您最好使用预编译的标题仍然是最快的编译。

    You're best off using precompiled headers still for fastest compilation.

    您也可以在gcc中使用预编译的头文件。 请参阅此处

    You can use precompiled headers in gcc as well. See here.

    编译预编译头将具有以 .gch 而不是 .pch 附加的扩展名。

    The compiled precompiled header will have an extension appended as .gch instead of .pch.

    所以,例如,如果你预编译stdafx.h,你将有一个预编译的头,将自动搜索调用 stdafx.h.gch code> stdafx.h

    So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called stdafx.h.gch anytime you include stdafx.h

    示例:

    stdafx.h :

    stdafx.h:

    #include <string>
    #include <stdio.h>
    

    a.cpp:

    #include "stdafx.h"
    int main(int argc, char**argv)
    {
      std::string s = "Hi";
      return 0;
    }
    

    然后编译为:


    > g ++ -c stdafx.h -o stdafx.h.gch

    > g ++ a.cpp

    > ./a.out

    即使在步骤1之后删除stdafx.h,您的编译也将正常工作。

    Your compilation will work even if you remove stdafx.h after step 1.

    这篇关于在跨平台代码中处理stdafx.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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