ode*_*dsh 5 c++ mingw mingw32 c++11
我写过最小的测试问题:
#include <sys/types.h>
int main(int argc, char** argv) {
off_t l = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下作品: 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
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么会这样?这困扰我的原因是我试图在我的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)
Run Code Online (Sandbox Code Playgroud)
当使用选项-std = c ++ 11时,编译器定义了-D__STRICT_ANSI__,它删除了off_t的定义,只留下_off_t定义.
编译器是正确的,因为off_t不符合标准.
这对我来说很困惑,因为我想要像nullptr和lambdas这样很酷的东西使用c ++ 11.我根本不关心STRICT_ANSI.
解决方案是使用-std = gnu ++ 11
(另一种选择是只需要为需要它的头文件typedef off_t typedef _off_t off_t;)