Quick question. If I compile my program on a Win32 machine, with the appropriate ifdef’s will that program run on a linux, or mac without recompiling on those machines? Or must I recompile the code to work with those machines?
You have to recompile. #ifdef s are only compiler directives.
…and there are usually some rules that you need to obey in order for a recompile to work on different platforms. Here are a few:
-
Only use portable APIs & functions (e.g. OpenGL, GLUT, GLFW, libMikMod, FMOD, fopen/fread/fwrite/malloc/strcmp etc).
-
NEVER rely on machine endianity (big vs little endian), or integer sizes (e.g. ‘long’ can be 32 or 64 bits depending on architecture)
-
Avoid ugly castings (e.g. long <-> void*)
-
Don’t read/write binary data (integers, structs etc) directly to/from files. Use well defined file formats and use explicit parsing in your code
-
If possible, try to compile your code on different operating systems/machines (e.g. if you’re running Windows, download and install Linux as a secondary boot option - Linux Mandrake is easy to install)