Skip to main content
GameDev.net gamedev.net
🔒 Locked

C\C++ basic question...

Started by Ancient Spirit Sep 17, 2005 at 6:21 AM 14 replies 600+ views
Original Post
Ancient Spirit
Ancient Spirit
Hello, i recall seeing somewhere a #ifdef to check the system that is running now... Does anyone knows how to check for the system that it is compiled on? If you wonder why... I want to be able to compile on linux and windows without changing code or include files.

if OS is Linux
    #include <ncurses.h>
else
    #include <graphics.h>

something like that
This world is ruled by big furry CATS!! :)Meow! :)
dave
dave
I think it is:

#ifdef WIN32
...
#else
...


Hope that helps,

ace
rip-off
rip-off
what are you doing?

if you're into graphics sound and stuff and don't want to change your source code use SDL ( www.libsdl.org )

if you're careful you can make your code work on just about anything using SDL
Ancient Spirit
Ancient Spirit
Quote:
Original post by ace_lovegrove
I think it is:

#ifdef WIN32
...
#else
...


Hope that helps,

ace


thanks! do you know the one for unix/linux aswell and for MacOS and BeOS? :)
Sorry if i'm being too rude...

This world is ruled by big furry CATS!! :)Meow! :)
Ancient Spirit
Ancient Spirit
Quote:
Original post by rip-off
what are you doing?

if you're into graphics sound and stuff and don't want to change your source code use SDL ( www.libsdl.org )

if you're careful you can make your code work on just about anything using SDL


it was just an example, relax :)
This world is ruled by big furry CATS!! :)Meow! :)
Ancient Spirit
Ancient Spirit
Quote:
Original post by Captain Nuss
Since #ifdef is a prepocessor directive, it will only be evaluated at compile time. Or, more specifically, just before the actual compiler compiles the C/C++ code to assembly. Thus, #ifdef is exactly what you'll want.


yea, my problem is that i dont know what should come after the #ifdef :)
This world is ruled by big furry CATS!! :)Meow! :)
dave
dave
For Linux try either:

#ifdef linux

or

#ifdef HAVE_LINUX

Those are the only ones i saw on google wheni searched,

ace
vNistelrooy
vNistelrooy
_WIN32 is a preprocessor macro that YOU have to define when you are compiling for a Windows platform. Visual C++ defines it for you. Check the project settings. #ifdef is a preprocessor directive. You can't compile an application to work both on Linux and Windows (natively). You compile an executable for Windows and another one for Linux.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Ancient Spirit
Ancient Spirit
Quote:
Original post by vNistelrooy
_WIN32 is a preprocessor macro that YOU have to define when you are compiling for a Windows platform. Visual C++ defines it for you. Check the project settings. #ifdef is a preprocessor directive. You can't compile an application to work both on Linux and Windows (natively). You compile an executable for Windows and another one for Linux.


sorry if i didn't explain myself wee enough, vNistelrooy, what i ment is that i want to be able to compile it, not launch it on other os's...
This world is ruled by big furry CATS!! :)Meow! :)
vNistelrooy
vNistelrooy
Quote:
Original post by ace_lovegrove
For Linux try either:

#ifdef linux

or

#ifdef HAVE_LINUX

Those are the only ones i saw on google wheni searched,

ace


When compiling for Linux predefine (-D) _LINUX using gcc.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
vNistelrooy
vNistelrooy
Quote:
Original post by Ancient Spirit
Quote:
Original post by vNistelrooy
_WIN32 is a preprocessor macro that YOU have to define when you are compiling for a Windows platform. Visual C++ defines it for you. Check the project settings. #ifdef is a preprocessor directive. You can't compile an application to work both on Linux and Windows (natively). You compile an executable for Windows and another one for Linux.


sorry if i didn't explain myself wee enough, vNistelrooy, what i ment is that i want to be able to compile it, not launch it on other os's...


Again, when you are compiling for Windows #define _WIN32 (or _WIN, or _WINDOWS or _MS_SUX or whatever you like) then #ifdef whenever you want to do some OS specific stuff. Same about Linux.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Ancient Spirit
Ancient Spirit
Quote:
Original post by Captain Nuss
Quote:
Original post by Ancient Spirit
yea, my problem is that i dont know what should come after the #ifdef :)

Ah, I see. From reading your post, I got the impression you thought that #ifdef would be evaluated at runtime. :)


i'm not THAT n[00]bish :)
This world is ruled by big furry CATS!! :)Meow! :)
Ancient Spirit
Ancient Spirit
ok, thank you all, i got it :)
This world is ruled by big furry CATS!! :)Meow! :)
SiCrane
SiCrane
For a list of macros predefined by the preprocessor you might want to check out this site.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.