5

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?

Kelvin Shadewing
  • 303
  • 2
  • 16

2 Answers2

2

realpath() is a POSIX function which is why it is not found by MinGW. What you could do is use the boost::filesystem library as a cross-platform alternative to find the absolute path. A tutorial for boost::filesystem is available below:

http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v2/doc/index.htm#tutorial

There have already been some questions on the boost::filesystem library. A link to one which may be useful has been attached below:

How do I "normalize" a pathname using boost::filesystem?

James Balajan
  • 301
  • 4
  • 13
0

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.
Chester Gillon
  • 326
  • 2
  • 4