none
Linux development with VS: using precompiled headers with gcc RRS feed

  • Question

  • Can I use precompiled headers when developing for Linux with Visual Studio?

    The answer here on SO makes me think it should be possible, but I do not know how I can make VS issue the command line

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

    with the same parameters (include directories, C++ standard etc.) as for the .cpp files. I tried a dirty trick by compiling stdafx.cpp into stdafx.h.gch, but could not fool gcc.

    Thursday, June 14, 2018 2:53 PM

Answers

  • Hi,

    thanks for posting here.

    >>Can I use precompiled headers when developing for Linux with Visual Studio?

    For this case, I suggest you try these ways below.

    1. Use GCC with precompiled headers. To create a precompiled header file, simply compile it as you would any other file, if necessary using the -x option to make the driver treat it as a C or C++ header file.

    For more information, please refer to this document below.

    https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

    2. Use tools like CMake to set up precompiled headers. Here is a sample document for you as a reference.

    https://github.com/larsch/cmake-precompiled-header

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Guido Franzke Friday, June 15, 2018 8:01 AM
    • Marked as answer by hpetschko Friday, June 15, 2018 8:25 AM
    Friday, June 15, 2018 7:35 AM

All replies

  • Hi,

    thanks for posting here.

    >>Can I use precompiled headers when developing for Linux with Visual Studio?

    For this case, I suggest you try these ways below.

    1. Use GCC with precompiled headers. To create a precompiled header file, simply compile it as you would any other file, if necessary using the -x option to make the driver treat it as a C or C++ header file.

    For more information, please refer to this document below.

    https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

    2. Use tools like CMake to set up precompiled headers. Here is a sample document for you as a reference.

    https://github.com/larsch/cmake-precompiled-header

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Guido Franzke Friday, June 15, 2018 8:01 AM
    • Marked as answer by hpetschko Friday, June 15, 2018 8:25 AM
    Friday, June 15, 2018 7:35 AM
  • Hi Baron Bi,

    thanks for your answer. I haven't used CMake so far and would prefer to stick to the setup that I get when using the Visual C++/Cross Platform/Linux/Console Application (Linux) template as long as I do not encounter too severe limitations.

    I think I can make GCC create the .gch file as the Remote Pre-Build Event, provided this event is triggered after copying the files. I will test that and report later.

    For those willing to use CMake I will mark your reply as answer.

    Regards

    Hansjoerg

    Friday, June 15, 2018 8:25 AM
  • I did a little bit more research and found the following:

    It is possible to tell MSBuild to compile stdafx.h by changing its item type from 'C++ header' to 'C++ compiler'. By setting the 'Object File Name' value, it's also possible to create stdafx.h.gch in the same location as stdafx.h.

    The result however is not accepted as valid precompiled header by GCC, and here is the reason why: the command line for the generation of the precompiled header file must either look like this
        g++ -c stdafx.h -o stdafx.h.gch

    or like this

        g++ -c -x c++-header stdafx.h -o stdafx.h.gch

    Unfortunately, the issued command line always looks like this
        g++ -c -x c++ stdafx.h -o stdafx.h.gch
    and thus makes GCC create a normal object file instead of a precompiled header, even though its file extension is gch.

    I haven't found a way yet to enforce the correct switch '-x c++-header'.
    Strangely, it is possible to use precompiled headers for Android projects. What could be the reason for this omission?



    • Edited by hpetschko Friday, June 15, 2018 2:26 PM
    Friday, June 15, 2018 2:24 PM