i need to create a project that able to run both under linux and windows(visual studio)... i pretty much finish all coding and able to run both windows and linux in separate files
the problem i deal with is the header file
which visual studio required #include "StdAfx.h" and not found in G++ under linux

i tried to #ifndef and #ifdef to fix it in this way, but visual studio still gives me a compiler error. so how should fix to make it works under both in Linux(g++) and windows(visual studio)?


#ifdef WIN32 (also tried ifndef)
#include "StdAfx.h"
....
#endif

codes...

Recommended Answers

All 8 Replies

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 environments. Don't use any of Visual Studio's cutesy enhancements at all.

but if I don't use stdafx. vs would give me a precompiled header error.so how would I fix this problem without including stdafx? btw, thanks for helping me out in the last thread

it is a win32 console project..

but if I don't use stdafx. vs would give me a precompiled header error.

You can turn off precompiled headers in the project settings.

it is a win32 console project..

Anything except an empty project will add Microsoftisms. When you're trying to write portable code, I'd recommend starting with an empty project.

Yes, when you make a project in VC++ untick the box "use precompiled headers". You can delete stdafx.h and then set "Precompiled Header Support" (something along those lines to NO in the project properties page.

thanks all!! i just see the "precompiled headers"
works perfectly fine now!

I know this post is over 2 years old but I really hate it wen people post answers who don't know what they are talking about. precompiled headers are NOT microsoftism they are part of C++ and will work on MS, Linux and anything else that compiles C++. The point of them is very simple. They only provide a benefit tho on VERY large projects that rely on common headers that do not change. Precompiled headers simply compile headers that rarely change that are used a lot thoughout your project to speed upcompilation. a good example is Squid. If you have ever compiled squid try doing so without precompiled headers it will triple the compile time.

precompiled headers are NOT microsoftism they are part of C++

That's half-true. Precompiled headers are certainly not part of standard C++, in the sense of what the ISO standard document describes (which is admitedly also the case for most practical compilation mechanisms). However, it is a common feature that many C++ compilers provide. And in that sense, the feature itself is not a microsoftism. But then again, the way that MSVC implements precompiled headers (using the stdafx.h header) is unique to MSVC and is certainly not portable (and is quite impractical and annoying). Other compilers implement this feature differently, and in much less intrusive manner (just a command-line option to switch it on for all headers). And some compilers don't implement it at all.

If you want to use pre-compiled headers in a cross-platform, portable library or application, then you will have to rely on a cross-platform build system, such as cmake, to handle the specific method different compilers use.

And yes, unless you have a large project, there is no point in using pre-compiled headers, but when you do have a large project, they can be very beneficial to speed up compilation.

I know this post is over 2 years old but I really hate it wen people post answers who don't know what they are talking about.

Me too. Especially people who bump posts with a strawman argument to prove that they know what they're talking about. Those are the worst. ;)

precompiled headers are NOT microsoftism

If you read carefully, you'll find that mention of Microsoftisms was made only once in reference to Visual Studio projects. If you want to read into it, the discussion is about stdafx.h and the Use Precompiled Headers option of Visual C++ specifically rather than precompiled headers in general. Last I checked, that particular implementation of precompiled headers is indeed specific to Microsoft's C++ compiler. Unless you were thinking of a compiler that can handle stdafx.h without explicitly building your own to make it work, I don't think calling it a Microsoftism is unreasonable.

they are part of C++ and will work on MS, Linux and anything else that compiles C++.

They're not part of C++ by my understanding of the ISO standard. However, compiler support is good. The problem is that compiler support is different, and therefore there are hoops you have to go through to port code written using precompiled headers to another compiler. Most of the time when someone asks about stdafx.h, they really have no need for precompiled headers in the first place, thus my recommendation of an empty project for portability sake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.