0

I'm writing a program for unix and windows, I need definition for folder separator like this: #define FOLDER_SEPARATOR "/", I need to understand at compile time, which system is working, how I can understand this? I need something like this:

 #if defdefine WINDOWS
 #define FOLDER_SEPARATOR "\"
 #elif defined UNIX
 #define FOLDER_SEPARATOR "/"
 #endif

Tell me, is there something like that in the C language, now I'm writing under gcc and use plain Makefile, but I need something cross-platform.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15
  • Look here: http://sourceforge.net/p/predef/wiki/OperatingSystems/ – cadaniluk Aug 29 '15 at 19:57
  • The standard does not specify such a macro. YOu have to check the OS and define yourself. As you are using make, perhaps it is better to use this to check, as there may be other differences, e.g. platform-abstraction libraries. – too honest for this site Aug 29 '15 at 20:00

1 Answers1

2

The macro is not available by itself, but there are a couple of preprocessor symbols that are useful:

 #ifdef _WIN32    //Always defined also on X64 OS
 #define FOLDER_SEPARATOR "\\"
 #elif defined (__linux__)  //Normally defined by linux systems  
 #define FOLDER_SEPARATOR "/"
 #else
 #error Unknown system!
 #endif

It's a common way to define a value that can be concatenated at compile time to strings in C. I.e. if you have:

sprintf(fullpath, "C:" FOLDER_SEPARATOR "%s" FOLDER_SEPARATOR "%s", path, file);

The C compiler will create a single format string:

"C:\%s\%s"  //single backslash because it is already in string
Frankie_C
  • 4,764
  • 1
  • 13
  • 30