0

I want to set the file path based upon the operating system in C++.

I've researched ways to do this with a preprocessor call but do not know how this is going to help me in setting the file path in the program itself.

The pseudocode is roughly as follows:

string file_path; if WINDOWS_OS: file_path = <windows file path> else if MAC_OS: file_path = <mac file path>

I'm hoping someone smarter than myself will be able to guide me to the best way in going about accomplishing this. Thanks!

B Stoops
  • 111
  • 1
  • 3

1 Answers1

0

The preprocessor runs at compile time. Once the compiler completes, a final executable version of the program is completed. That executable will only run on one platform (Windows or Mac or Linux).

So, it is not necessary to check which host type you are on during runtime.

The code you have written appears to check the host type while the code is running. That is not necessary or appropriate in C++. A language like c# which creates Byte-Code which can be interpreted on any of the three platforms would check the host type at run time, but not C++.

Here is an example header which checks for host type, at compile time, using the preprocessor (taken from How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?):

#ifdef _WIN32
   //define something for Windows (32-bit and 64-bit, this part is common)
   #ifdef _WIN64
      //define something for Windows (64-bit only)
   #else
      //define something for Windows (32-bit only)
   #endif
#elif __APPLE__
    #include "TargetConditionals.h"
    #if TARGET_IPHONE_SIMULATOR
         // iOS Simulator
    #elif TARGET_OS_IPHONE
        // iOS device
    #elif TARGET_OS_MAC
        // Other kinds of Mac OS
    #else
    #   error "Unknown Apple platform"
    #endif
#elif __linux__
    // linux
#elif __unix__ // all unices not caught above
    // Unix
#elif defined(_POSIX_VERSION)
    // POSIX
#else
#   error "Unknown compiler"
#endif

I am still a little confused by your question. Are you asking for the correct method of setting the file path for each operating system?

Gardener
  • 2,591
  • 1
  • 13
  • 22