Skip to main content
gamedev.net
🔒 Locked

code for linux, windows and mac

Started by coms Sep 27, 2005 at 10:55 PM 8 replies 1.4k views
Original Post
coms
coms
Hey, i wanted to ask if anyone has coded openGL code that works both for linux, windows and mac. I've figured out the main differences, i also use glut library. So the question is: how do you write source that can be compiled cross platform. i've seen some solutions with #ifdefs but i'm totally unsure how to use these, googling around didn't yield any significant results. help me our?
Doc
Doc
Using a cross-platform library is usually a good bet. It means all the hard #ifdef work has already been done for you. Just so long as you write code that's platform agnostic (which isn't hard to do) you shouldn't need to write any of your own #ifdef blocks of that sort.

GLUT's probably a good start. SDL is a good one, too, and it lets you set up an OpenGL window pretty easily.

If you find yourself #including or along those lines you've done something wrong.
coms
coms
hey, thanks for the advice.
But let me clarify, i want to learn how this #ifdef stuff works, and how to make certain sourcecode crossplatform.

i know glut, has libraries for differnet platforms, however the problem arises with compilation of code when you need to include thse libraries.
Exampe:
// In linux#include <X11/Xlib.h>#include <GL/glx.h>#include <GL/gl.h>#include <GL/glut.h>


// In windows   #include <glut.h>   #include <stdio.h>

So, when i need to compile stuff, i would want it to automaticly include libraries depending on the platform
igni ferroque
igni ferroque
An alternative to littering code with ifdefs is to isolate platform-specific parts into separate source files that share a common interface (function prototypes). You then only compile and link the file that is appropriate for the platform you are building for.
Free Mac Mini (I know, I'm a tool)
coms
coms
Well, it is suitable for small projects, but i don't think it is very appropriate for large things.
Plus there are not that many things that are platform specific.
So any pointer on howto properly use ifdefs?


% cat 1.c
int main (){
int name=0;
#ifdef LINUX
name=1;
#endif
#ifdef WIN32
name=2;
#endif
printf("name %i\n", name);
}


does not seem to work
% gcc 1.c -o out.out
% ./out.out
name 0
MaulingMonkey
MaulingMonkey
Just on the subject of #ifdefs:

Take this code slice:

#ifdef DEBUG    cout << "Here is a debug message" << endl;#else //def DEBUG    cout << "This isn't a debug message" << endl;#endif //ndef DEBUG


This will compile to:

cout << "Here is a debug message" << endl;


Assuming DEBUG is defined. If it's not, it'll compile to the other thing.

DEBUG can even be defined into nothingness - so it'll pop up if you've had statements like this:

#define DEBUG
#define DEBUG 13
#define DEBUG I like pie

And won't if you've had statements like this:

#undef DEBUG

(whichever being more recent being the one that applies)

The magic usually comes in when your compiler defines stuff for you. For example, If I run GCC like so on my windows box:

g++ source.cc -DDEBUG

Not only will DEBUG be defined, but WIN32 and some other junk as well. A nice mix of some stuff that's automatically supplied by the environment, and some stuff you can configure yourself.


Of course, the standard says zero zip nada on such #defines and what will or will not be automatically defined, so you'll either have to depend on yourself to define them, or your compiler's specific behavior (which means google searches for details as to what your compiler does define should include your compiler name and version.
coms
coms
Okay, do you see anything wrong with the code i posted?
It is compiled on linux Red Hat Enterprise Linux AS release 3 (Taroon Update 5)
The enviroment does not appear to be set, or something. What am i doing wrong?
lazarus07
lazarus07
@coms:
you should use #ifdef linux (linux in lowercase letters),
then you get the expected result.

servus
coms
coms
OHHHHHH, thank you!
Nitage
Nitage
For windows use _WIN32 instead of WIN32 (with the underscore).


I don't use any #ifdef in cross platform code.

I just have shared header files with the interface in them, and the in the project settings I link the appropriate library:

e.g.

//Common headers for all targets
window.hpp
message_box.hpp

//seperate libraries for each target
platform_Win32.lib
platform_X11.lib
platform_Mac.lib

So the C++ code doesn't change, only the library I choose to include in the project.

Topic Locked

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

Sign in to reply to this topic.