Compiler switch for selecting win/linux

Discussion about everything. New games, 3d math, development tips...
Post Reply
miko93
Posts: 54
Joined: Sat Nov 12, 2005 5:24 pm
Location: Regensburg, Germany
Contact:

Compiler switch for selecting win/linux

Post by miko93 »

Hi there,

for my game, I use the following code for selecting compilation for windows or linux:

Code: Select all

// global.h

#define MY_PLATFORM_WIN32 1
#define MY_PLATFORM_LINUX 2

// ====================================================
// Change here to compile for another platform

//#define PLATFORM MY_PLATFORM_LINUX
#define PLATFORM MY_PLATFORM_WIN32

// ============================================

#if PLATFORM == MY_PLATFORM_WIN32
	#include <Windows.h>
	[...]
#else
	#include <stdio.h>
	[...]
#endif
As seen, I have to change manually when switching development platforms. Of course, this isn't that bad.
I recon there could be an easy way to achieve this in an automated manner, tho. Any hints? Thanks.
"Yessir, I'll be happy to make these unnecessary changes to this irrelevant document." (Dilbert)
AlexL
Posts: 184
Joined: Tue Mar 02, 2004 6:06 pm
Location: Washington State

Post by AlexL »

In my applications, I use something similer to the following; though I beleive Irrlicht also has a way to check if it is running in Linux, Windows or MacOSX. The defines for _WIN32 and _DEBUG are all part of the compiler so there is nothing special you need to do.

You could also do something like this in your main file to select if it should be a WinAPI main or just a normal one. I hope this gives you an idea of how else you can do this :)

Code: Select all

//IGlobals.h
#ifdef _WIN32
	#include <windows.h>
	#define WIN32_LEAN_AND_MEAN
	#pragma comment(linker,"/ENTRY:WinMainCRTStartup")
	#ifdef _DEBUG
		#pragma comment(linker,"/subsystem:console")
	#endif
#else
	#include <unistd.h>
#endif

//Main.cpp
#ifdef _WIN32
	int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int nCmdShow)
#else
	int main(int argc,char* argv)
#endif
{
	return false;
}

miko93
Posts: 54
Joined: Sat Nov 12, 2005 5:24 pm
Location: Regensburg, Germany
Contact:

Post by miko93 »

AlexL, thanks a lot.
"Yessir, I'll be happy to make these unnecessary changes to this irrelevant document." (Dilbert)
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

AlexL wrote:In my applications, I use something similer to the following; though I beleive Irrlicht also has a way to check if it is running in Linux, Windows or MacOSX. The defines for _WIN32 and _DEBUG are all part of the compiler so there is nothing special you need to do.

You could also do something like this in your main file to select if it should be a WinAPI main or just a normal one. I hope this gives you an idea of how else you can do this :)

Code: Select all

//IGlobals.h
#ifdef _WIN32
	#include <windows.h>
	#define WIN32_LEAN_AND_MEAN
	#pragma comment(linker,"/ENTRY:WinMainCRTStartup")
	#ifdef _DEBUG
		#pragma comment(linker,"/subsystem:console")
	#endif
#else
	#include <unistd.h>
#endif

//Main.cpp
#ifdef _WIN32
	int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int nCmdShow)
#else
	int main(int argc,char* argv)
#endif
{
	return false;
}

Are you using the same compiler on all platforms? Or Visual Studio on Windows, GCC on Linux etc?

At some point I need to remove the Visual Studio project files from my project and see if I can use one makefile for all platforms.
AlexL
Posts: 184
Joined: Tue Mar 02, 2004 6:06 pm
Location: Washington State

Post by AlexL »

Yes, I am using Visual Studio .NET on Windows and GCC on Linux; though I have also used this with GCC on Windows through Dev-Cpp. So there's no problem there :)
Post Reply