Handling stdafx.h in cross-platform code

stdafx.h download
precompiled headers
gcc precompiled headers
how to use precompiled headers
can t create precompiled header
precompiled gcc
gch extension
gcc compile header

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?

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.

c++ - Handling stdafx.h in cross-platform code, 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. Handling stdafx.h in cross-platform code . Posted by: admin November 20, 2017 Leave a comment. Questions: I have a Visual Studio C++ based program that uses pre

I used option 3 last time I needed to do this same thing. My project was pretty small but this worked wonderfully.

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  Handling stdafx.h in cross-platform code (6) 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'd either go for option 4 or option 2. I've experimented with precompiled headers on both various VS versions and GCC on Linux (blog posts about this here and here). In my experience, VS is a lot more sensitive to the length of the include paths, number of directories in the include path and the number of include files than G++ is. When I measured build times properly arranged precompiled headers would make a massive difference to the compile time under VS whereas G++ was pretty much unimpressed by this.

Actually, based on the above what I did the last time I worked on a project where this was necessary to rein in the compile time was to precompile the equivalent of stdafx.h under Windows where it made sense and simply used it as a regular file under Linux.

StdAfx.h, The *.pch file is created as a result of the stdafx.cpp file's compilation. However, there is an easier way to handle precompiled headers. The only thing to actually do is to include the path containing the stdafx.h (or precompiled header) in the default include path list. This is needed because the MS compiler actually replaces the #include "stdafx.h" with the precompiled data without really looking for the header. Other compilers will usually want to pull in the data.

Very simple solution. Add a dummy file entry for "stdafx.h" in Linux environment.

Use of "stdafx.h" - C++ Articles, 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  A file named stdafx.h is automatically generated when I start a project in Visual Studio 2010. I need to make a cross-platform C++ library, so I don't/can't use this header file. What is stdafx.h used…

I would only use option 1 in a big team of developers. Options 2, 3, and 4 will often halt the productivity of other members of your team, so you can save a few minutes a day in compile time.

Here's why:

Let's assume that half of your developers use VS and half use gcc. Every now and then some VS developer will forget to include a header in a .cpp file. He won't notice, because the stdafx.h implicitly includes it. So, he pushes his changes in the version control, and then a few other members of the gcc team will get compiler errors. So, for every 5 minutes-a-day you gain by using precompiled headers, 5 other people waste by fixing your missing headers.

If you don't share the same code across all of your compilers, you will run into problems like that every day. If you force your VS developers to check for compilation on gcc before pushing changes, then you will throw away all your productivity gains from using precompiled headers.

Option 4 sounds appealing, but what if you want to use another compiler at some point in time ? Option 4 only works if you only use VS and gcc.

Notice that option 1 may make gcc compilation suffer a few seconds. Although it may not be noticeable.

make code run both under visual studio and linux g++?, Precompiled Header stdafx.h is basically used in Microsoft Visual Studio to let If you are including the below windows header files. Code: Reduces Unnecessary Processing. Unix/Linux programming · Source Code · Standard Library · Tips and Tricks · Tools and Libraries · Visual C++ · Windows API. Handling stdafx.h in cross-platform code | ASK AND ANSWER on Jan 15, 2016 […] the approach from here – compile VC++ with /FI to implicitly include stdafx.h in each cpp file. Therefore in VS your […]

Precompiled header, Why are you using StdAfx.h? That's your major problem. Write the code based on the C++ Standard and the program should compile in both  Windows 10 VSCode: 1.29.1 C/C++ Extension Version: 0.20.1 No other extensions installed. Background: the vscode-cpptools plugin is not correctly resolving default compiler defines in my cross-platform code base (separate issue).

StdAfx.h : cpp, The /Yu option, subordinate to the /Yc option if used together, causes the compiler to make used of already precompiled code from such a file. pch.h (​named stdafx. A file named stdafx.h is automatically generated when I start a project in Visual Studio 2010. I need to make a cross-platform C++ library, so I don't/can't use this header file. I need to make a cross-platform C++ library, so I don't/can't use this header file.

SAS/C Cross-Platform Changes for Release 7.50 : Release 7.50 , projects, instantly makes code non-cross platform by #including MS-specific headers, The name, stdafx.h, doesn't even make any sense as it refers to an old pre-MFC By this I mean that they handle errors correctly (What if the user types  Managed code means that it runs in the .NET framework, which means you can access those classes ( which also means you're not using standard C++ anymore, although standard C++ is available to you, the application relies on the .NET framework on the target machine, and as a result, you can use the framework ), and you can mark things to be

Comments
  • I meant just that after you run g++ -c stdafx.h -o stdafx.h.gch, you can remove stdafx.h and your compilation would still work (g++ a.cpp). You wouldn't actually do this, but I mentioned it as proof that it is using the precompiled header.
  • @Brian Maybe I'm wrong but this looks like option 4 I listed :) I tried to remove stdafx.h from a.cpp and it didn't compiled - precompiled header will be searched only if respective header file is included. I confirmed it with strace :)).
  • @idimba: Yes it is option 4, but you modified your question and added #4 three minutes after I posted my answer. The respective header file should be included anyway in source.
  • Let's not worry about my recent comment on a different question: The compiler for main just needs the function/class declarations (.hpp), not the implementations/definitions. The linker finds those. #includeing .cpp files can be done but is incredibly bizarre.