当使用c++11编译时,Mingw g++无法识别off_t。

5

我已经写了一个最简单的测试问题:

#include <sys/types.h>
int main(int argc, char** argv) {
    off_t l = 0;
    return 0;
}

以下内容可行:g++ test.cpp
但是,如果我尝试使用c++11编译,我会得到以下错误提示: c:\test>g++ -std=c++11 test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:5:2: error: 'off_t' was not declared in this scope
test.cpp:5:8: error: expected ';' before 'l'
test.cpp:6:9: error: 'l' was not declared in this scope

有人能解释一下这是为什么吗?我感到烦恼的原因是,我正在尝试在我的C++11代码中使用zlib库。库zlib.h经常使用off_t。

我的编译器版本是:gcc 4.7.2

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.7.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.7.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-with-cxx --enable-version-specific-runtime-libs -build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.7.2 (GCC)

1
当我比较以下两个命令时:g++ -E test.cpp > test.cg++ -std=c++11 -E test.cpp > test.c我可以看到,如果没有加上c++11标准,代码中会有以下定义:typedef long _off_t; typedef _off_t off_t;而使用了std=c++11标准后,只有以下定义:typedef long _off_t; - odedsh
好的,这是由于在使用-std=c++11时定义了__STRICT_ANSI__。我可以从这里开始解决。 - odedsh
1个回答

5
使用选项-std=c++11时,编译器定义了-D__STRICT_ANSI__,这将删除off_t的定义,只保留_off_t的定义。
编译器这样做是正确的,因为off_t不符合标准。
这让我感到困惑,因为我想使用c++11的酷炫功能,如nullptr和lambda表达式。我一点也不关心STRICT_ANSI。
解决方案是使用-std=gnu++11。
(另一个选择是仅为需要它的头文件typedef off_t typedef _off_t off_t;)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接