# including windows.h but cross-platform?

This topic is 4155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I'm attempting to make my game easier to port when it comes to different platforms other than Windows. Although, to use OGL I need to include <windows.h>. Is there a way I can include <windows.h> through my compiler rather than the source code, so that the source code won't need to be modified to be recompiled on another platform. All the platform specific code (setting up the window etc...) is in a seperate project. I'm using VC2k5. Thank you! (: NB

##### Share on other sites
What happened to my question!?!?! Ok... try again:

I'm wanting to make my game easier to port to different platforms other than Windows. To use OGL I need to include <windows.h>, although this then removes the cross platform idea of the game code. Is there a way I can include the header from the compiler without changing the code, so that way I can recompile the game code to a static library for whatever OS? My compiler is VS2K5, the platform specific code (setting up the window etc..) is in a seperate project.

Thank you! (:
NB

##### Share on other sites
I don't know about including it through the compiler, but there is a way to include it through source code and not worry about changing the code when you're on a different compiler.

For msvc, you can include like this:

#ifdef _MSC_VER#  include <windows.h>#endif

_MSC_VER is only defined with visual studio, so the include will only be processed if that is defined.

##### Share on other sites
It might be better to use

#ifdef WIN32 // instead of _MSC_VER
# include
#endif

Since you'll likely want windows.h included on Windows, regardless of which compiler is used.

##### Share on other sites
Hrm... some reason viewing the question only shows my reply, I'm viewing this through the "reply to topic" screen. :S

One thing, maybe I could do this:

#define WIN32_VS2K5#ifdef WIN32_VS2K5#include <windows.h>#endif#ifdef WIN64_VS2K5#include <windows.h>#endif#ifdef LINUX32_SOMELINUXCOMPILER#include SOMELINUXHEADER#endif#ifdef LINUX64_SOMELINUXCOMPILER#include SOMELINUXHEADER#endif

##### Share on other sites
Or do away with preprocessor defines all together. Include one file, unconditionally. Setup your compiler to include the version of that file appropriate for the current environment.

CM

##### Share on other sites
The above posted define will work for VS, but this is a better method

Quote:
 #if (defined(__WIN32__) || defined(_WIN32) || defined(WIN32))#include #endif

##### Share on other sites
Else you can take a look at the glut.h header file. It totally removes the windows.h dependency.

glut.h only defines the few Windows specifics required, and saves noticeable compilation time even when you work in the Windows context. This used to be a great advantage some years ago, but nowadays CPUs and compilers are so fast, it's not really an argument anymore ...

Still if you do such a choice, which seems rather drastic, you must see it on a broader scale, as a discipline. Be accustommed to separate dependencies (concepts, compilation and linking) and split the project in well formed modules, well interfaced. I'd say it really pays on medium term.

I have created many decent multi OS projects based on GL, and this scheme works quite well. It's very impressive and enjoyable to recompile a complex project developped many weeks on Windows and have it working fine on MAC OS in 5 minutes !

Anything OS dependent put separately in a library, with a modular approach (minimalist system server) and you have clean totally OS independent stuff for your core game engine project.

##### Share on other sites
Hmm... Ok, thanks everybody for your replies. A lot of things there to look over here...

I'm quite interested in glut.h as well; never really thought anything of that other than an outdated wrapper. I'll take a good butchers.

Thanks again all,
NB (:

##### Share on other sites
Quote:
Original post by CmpDev
The above posted define will work for VS, but this is a better method

Quote:
 #if (defined(__WIN32__) || defined(_WIN32) || defined(WIN32))#include #endif

That depends... For example if you're using gcc even on a windows platform, you don't need to include windows.h to use opengl.

##### Share on other sites
Quote:
 Is there a way I can include ...

I was only answering the OP's question, I didn't mention GL.

##### Share on other sites
If you want completely platform independant access to opengl, you should look into SDL.

It can create a window and handle input and more in a platform independant manner.

##### Share on other sites
Quote:
 Original post by rip-offIf you want completely platform independant access to opengl, you should look into SDL.It can create a window and handle input and more in a platform independant manner.

SDL was something I originally looked at, although while I can't deny it's a powerful library, I don't find it overly suitable for my needs. My Windows code I use (I don't use sound at the moment) at the moment is no more than 500 lines long, which does precisely what I want and I know exactly what is happening in it. I just find using such a library will be overkill for my needs. Thanks anyways for your suggestion! :)