简体   繁体   中英

mkdir Windows vs Linux

I have a problem while porting a Linux tool to Windows. I am using MinGW on a Windows system. I have a class which handles all the in/output and within is this line:

mkdir(strPath.c_str(), 0777); // works on Linux but not on Windows and when it is changed to
_mkdir(strPath.c_str()); // it works on Windows but not on Linux

Any ideas what I can do, so that it works on both systems?

#if defined(_WIN32)
_mkdir(strPath.c_str());
#else 
mkdir(strPath.c_str(), 0777); // notice that 777 is different than 0777
#endif

You should be able to use conditional compilation to use the version that applies to the OS you are compiling for.

Also, are you really sure you want to set the flags to 777 (as in wide open, please deposit your virus here)?

You can conditionally compile with some preprocessor directives, a pretty complete list of which you can find here: C/C++ Compiler Predefined Macros

#if defined(_WIN32)
    _mkdir(strPath.c_str());
#elif defined(__linux__)
    mkdir(strPath.c_str(), 0777);
// #else more?
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM