Bre*_*een 3 popen c++11 visual-studio-2015
我正在尝试以可移植的方式从 C++ 程序运行 gnuplot。具有讽刺意味的是,对于 WIN_32 我没有问题,但我的编译器(Visual Studio 2015)无法识别我尝试用于其他操作系统的 POSIX 命令 popen() 。C++11 中 popen() 不存在吗?是否有等效项或者我必须更改标准?
这在 Windows Visual Studio 2015 中编译并运行:
#include <stdio.h>
#include <stdlib.h>
int main()
{
_popen(" ", "w");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不能编译:
#include <stdio.h>
#include <stdlib.h>
int main()
{
popen(" ", "w");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误错误 C3861 'popen':
一天结束时未找到标识符,我想要类似的行为
#include <stdio.h>
#include <stdlib.h>
int main()
{
#ifdef WIN_32
_popen(" ", "w");
#else
popen(" ", "w");
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个程序在 Linux 和 Mac 上使用时可以用 g++ 重新编译,但我想在 Windows 上使用 msvc14 进行编译
popen()确实不存在于 C++(任何版本)中。它由 posix 定义,因此可在大多数类 UNIX 操作系统上使用。
popen()Windows 上没有该函数,但您可以使用等效的_popen()函数
MSVC 预定义了_WIN32常量,您可以将其用于条件编译,.eg
#ifdef _WIN32
_popen(" ", "w");
#else
popen(" ", "w");
#endif
Run Code Online (Sandbox Code Playgroud)