Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling stdafx.h in cross-platform code

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.

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

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

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

  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  
  3. 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.

  4. On Linux compile stdafx.h only as a precompiled header (mimic Visual Studio)

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

like image 649
dimba Avatar asked Jul 27 '09 23:07

dimba


People also ask

What is the use of Stdafx h in C++?

The file stdafx. h is usually used as a precompiled header file. Precompiled headers help to speed up compilation when a group of files in a project do not change.

What is precompiled code?

Precompiled code is useful during the development cycle to reduce compilation time, especially if: You always use a large body of code that changes infrequently. Your program comprises multiple modules, all of which use a standard set of include files and the same compilation options.

Are precompiled headers worth it?

The advantage of precompiled headers is that the huge system library header files and other files that do not change at all or infrequently only have to be parsed once per build. As all C compilers (that I know of) work on every .

How do you make a precompiled header?

Configure Visual Studio to create precompiled headerschoose "All Configurations", then go to C/C++ -> Precompiled Headers, choose "Create Precompiled Header", make sure stdafx. h is the header file to use, and leave the rest to the default value.


1 Answers

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

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

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

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

Example:

stdafx.h:

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

a.cpp:

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

Then compile as:

> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out

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

like image 165
Brian R. Bondy Avatar answered Sep 18 '22 05:09

Brian R. Bondy