mkdir Windows 与 Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10356712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mkdir Windows vs Linux
提问by MindlessMaik
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:
将 Linux 工具移植到 Windows 时遇到问题。我在 Windows 系统上使用 MinGW。我有一个处理所有输入/输出的类,里面是这一行:
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?
任何想法我能做什么,以便它在两个系统上都能工作?
采纳答案by gcochard
#if defined(_WIN32)
_mkdir(strPath.c_str());
#else
mkdir(strPath.c_str(), 0777); // notice that 777 is different than 0777
#endif
回答by Eric J.
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)?
另外,您真的确定要将标志设置为 777(如在完全开放的情况下,请将您的病毒存放在这里)?
回答by wkl
You can conditionally compile with some preprocessor directives, a pretty complete list of which you can find here: C/C++ Compiler Predefined Macros
您可以使用一些预处理器指令有条件地进行编译,您可以在此处找到一个非常完整的列表:C/C++ Compiler Predefined Macros
#if defined(_WIN32)
_mkdir(strPath.c_str());
#elif defined(__linux__)
mkdir(strPath.c_str(), 0777);
// #else more?
#endif