mingw:使用 -std=c++11 编译时找不到函数
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);
相关文章:
- C ++程序编译错误,找不到/访问文件
- LD:尝试编译项目时找不到架构x86_64符号
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- gcc:"在Windows上编译时找不到-lasound"
- Red Hat:使用<atomic>编译很好,但链接器找不到__atomic_store_16;什么库?
- 无法使用 mingw-w64 g++ 编译:找不到 <wchar.h>
- CMake 在编译 Google 的 protobuf 示例时找不到 protobuf
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- 从Windows 10编译VS2017在Windows 7和…上部署..找不到DLL
- Qt 模块(QtSerialPort)在Qt定制构建中找不到(针对ARM交叉编译)
- 在 Ubuntu 18.04 上,我编译的 cpp 可执行文件找不到 VTK7 依赖项
- 编译 GCC-5.0 OS X 狮子 dyld:找不到符号:__ZNKSt11logic_error4whatEv
- 运行程序时找不到共享对象库,但在编译过程中链接了它
- 在 OSX 上编译C++时找不到库
- Libnoise 不想编译:找不到 -lnoise
- 用ar编译的存档,手动编译,ld找不到
- 交叉编译找不到引用
- 使用gsl的g++编译:找不到库
- gcc编译-找不到-lmpir