tin*_*lyx 8 c++ mingw stdio popen c++11
我试图编译下面的代码(来自/sf/answers/33527231/).编译运行正常,如果我编译
$ g++ test.cpp
Run Code Online (Sandbox Code Playgroud)
但在使用-std=c++11开关时出错了:
$ g++ -std=c++11 test.cpp
test.cpp: In function 'std::string exec(char*)':
test.cpp:6:32: error: 'popen' was not declared in this scope
FILE* pipe = popen(cmd, "r");
^
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?
(我在mingw.org和WindowsXP64上使用mingw32 gcc4.8.1)
码:
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
int main() {}
Run Code Online (Sandbox Code Playgroud)
我认为这popen是因为不是标准的ISO C++(它来自POSIX.1-2001).
你可以尝试:
$ g++ -std=c++11 -U__STRICT_ANSI__ test.cpp
Run Code Online (Sandbox Code Playgroud)
(-U取消之前定义的宏,无论是内置还是随附-D选项)
要么
$ g++ -std=gnu++11 test.cpp
Run Code Online (Sandbox Code Playgroud)
(GCC 定义 __STRICT_ANSI__当且仅当在调用GCC时指定了-ansi开关或-std指定严格符合某些版本的ISO C或ISO C++ 的开关)
使用_POSIX_SOURCE/ _POSIX_C_SOURCEmacros是一种可能的选择(http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html).
| 归档时间: |
|
| 查看次数: |
5888 次 |
| 最近记录: |