Cross-platform alternative to this realpath definition?

Viewed 983

So, someone recently submitted some bug fixes to my project, and they also included this:

#ifdef _MSC_VER
    #ifndef PATH_MAX
    #define PATH_MAX _MAX_PATH
    #endif
    #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#endif

The problem is I'm using Code::Blocks with MinGW, so when I try to compile, it says that realpath() isn't defined at all. The submitter didn't provide any alternative, and when I try to search for realpath without MSVC, all I get is using realpath with MSVC. Does anyone have something I can use with MinGW instead?

2 Answers

I found the following adjustment on the #ifdef condition worked:

/* Create a realpath replacement macro for when compiling under mingw
 * Based upon https://stackoverflow.com/questions/45124869/cross-platform-alternative-to-this-realpath-definition
 */
#ifdef WIN32
    #define realpath(N,R) _fullpath((R),(N),PATH_MAX)
#endif

In both cases the generated executables were run on Windows 10. Tested using:

  • The MinGW64 native Windows mex compiler packaged by MATLAB 2021a.
  • The i686-w64-mingw32-gcc 7.3-win32 cross-compiler which runs on Linux and can generate 32-bit Windows executables.
Related