-1

I'd like to write and reuse same code in both Linux and Windows, especially the path constants.

fopen(base+"subfolder/abc.bin","wb")

The above code worked fine when I cross compiled for Windows in Linux.

But, I had to change the code to fopen(base+"subfolder\\abc.bin","wb") to compile it in windows directly.

Now I have added many dependencies to the software which would require cross compilation if I cross compiled my software, which I don't wanna do, so, I use precompiled binaries in both Windows and Linux versions of the software.

The issue now, I face is if I wanted to compile in Linux, I'd have to change the path which I don't want to do, as I'd have to maintain, two different branches of the software. Is there a way, akin to Java using Paths.get("abcd/def/hij.bin").toString(). I'm not just asking for a system call, a custom function which identifies underlying OS and changes the path string accordingly, will do.

I need both C and C++.

Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64

1 Answers1

1

Not sure if you managed to find your solution , but you can change the path/string according to the OS with the predefined macros here is example:

    string getFileName(const string& s) {

   char sep = '/';

#ifdef _WIN32
   sep = '\\';
#endif

   size_t i = s.rfind(sep, s.length());
   if (i != string::npos) {
      return(s.substr(i+1, s.length() - i));
   }

   return("");
}

Here is a link with more information on predefined macros.

du4ko
  • 101
  • 6