我写了一个最小的可能的测试题:
#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库。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)发布于 2013-10-29 19:10:36
当使用选项-std=c++11时,编译器定义了-D__STRICT_ANSI__,它删除了off_t的定义,只保留了定义的_off_t。
编译器这样做是正确的,因为off_t不符合标准。
这让我很困惑,因为我想要c++11做一些很酷的东西,比如nullptr & lambdas。我根本不在乎STRICT_ANSI。
解决方案是使用-std=gnu++11
(另一种选择是只对需要typedef _off_t off_t的头文件进行typedef _off_t off_t;)
https://stackoverflow.com/questions/19666666
复制相似问题