[英]mingw: function not found when compiled with -std=c++11
我試圖編譯下面的代碼(來自https://stackoverflow.com/a/478960/683218 )。 編譯運行正常,如果我編譯
$ g++ test.cpp
但是在使用-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");
^
知道發生了什么事嗎?
(我在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() {}
我認為這是因為popen
不是標准的ISO C ++(它來自POSIX.1-2001)。
你可以嘗試:
$ g++ -std=c++11 -U__STRICT_ANSI__ test.cpp
( -U
取消之前的宏定義,內置或提供-D
選項)
要么
$ g++ -std=gnu++11 test.cpp
(GCC 定義 __STRICT_ANSI__
當且僅當調用GCC時指定-ansi
開關或指定嚴格符合某些版本的ISO C或ISO C ++的-std
開關)
使用_POSIX_SOURCE
/ _POSIX_C_SOURCE
宏是一種可能的替代方法( http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html )。
只需在開頭添加:
extern "C" FILE *popen(const char *command, const char *mode);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.