问题
I have written the smallest possible test problem:
#include <sys/types.h>
int main(int argc, char** argv) {
off_t l = 0;
return 0;
}
The following works: g++ test.cpp
But If I try to compile with c++11 I get:
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
Anyone can explain why this is happening? The reason this is bothering me is that I am trying to use the zlib library in my c++11 code. The library zlib.h uses off_t a lot.
My compiler version is: 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:
When using the option -std=c++11 the compiler defined -D__STRICT_ANSI__ which removes the definition of off_t leaving only _off_t defined.
The compiler is correct to do that since off_t is not conforming to the standard.
This was confusing for me cause I wanted c++11 for the cool stuff like nullptr & lambdas. I didn't care about STRICT_ANSI at all.
The solution is to work with -std=gnu++11
(Another option is to just typedef off_t for the header files that need it typedef _off_t off_t;)
来源:https://stackoverflow.com/questions/19666666/mingw-g-does-not-recognize-off-t-when-compiling-with-c11