c macros

so i am making a game that makes files and directorys for saving and stuff like that. the only problem is that my paths will differ from os to os. are there any c pre proccessor macros that will do this for me?
You need to detect which OS you are in.
These are predefined macros in c/c++
http://sourceforge.net/apps/mediawiki/predef/index.php?title=Operating_Systems
Then do you stuff based on OS
Last edited on
could you please explain which macros i need? the site wasn't very clear on the decriptions
You are doing this the wrong way...

This kind of information should be part of the build process (makefiles and the like), not embedded in the code.


That site, BTW, is a gem on the internet, and it is very clear. If you want code that caters to a specific OS, then you wrap it in a macro that identifies that OS. For example:

1
2
3
#ifdef _WIN32
  #include <windows.h>
#endif 

Hope this helps.
i wasn't offending the site but its just probably more clear to you than it is to me. like if i showed my friends a bunch of c code they wouldnt get it even though it would be clear to me. alls im asking for are three macros like the one above for each os
If you're looking for macros that will build a file path automatically depedning on the operating system, there aren't any that come as part of the C language.

There are game development libraries (GUI) that can do this, but that wasn't what you were asking.

The sourceforge page linked earlier lists symbols that are predefined by each of a large number of compilers. If a particular symbol is defined, then you know you are compiling under that particular compiler as Duoas' example showed.

I suspect all you really want to know is if you're compiling under Windows or some Linux variant. Taking Duoas' example a step further:

1
2
3
4
5
6
 
#ifdef _WIN32
  #define PATH_SEPARATOR '\\'
#else
  #define PATH_SEPARATOR '/'
#endif 


Then use the value of PATH_SEPARATOR to construct your file or path name assuming your file or path name is the same and the only thing that is different is the path separator.


closed account (1yR4jE8b)
Newer versions of Windows accept the Unix path separator just fine.
@Duoas: a better approach would be to use environment variables, command line arguments, config files, etc.
That way different users may have different paths using the same executable.

wait... ¿are we talking just about the separator?
@abstractionAnon: i want to build my own path based on the os.
Topic archived. No new replies allowed.