用C ++ 11编译时Mingw g ++无法识别off_t [英] Mingw g++ does not recognize off_t when compiling with c++11
问题描述
我写了尽可能小的测试问题:
I have written the smallest possible test problem:
#include <sys/types.h>
int main(int argc, char** argv) {
off_t l = 0;
return 0;
}
以下作品:g++ test.cpp
但是,如果我尝试使用c ++ 11进行编译,则会得到:
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
任何人都可以解释为什么会这样吗?这困扰我的原因是我试图在我的c ++ 11代码中使用zlib库.库zlib.h经常使用off_t.
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.
我的编译器版本是:gcc 4.7.2
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)
推荐答案
使用选项-std = c ++ 11时,编译器定义了-D__STRICT_ANSI__,它删除了off_t的定义,仅定义了_off_t.
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.
由于off_t不符合标准,因此编译器是正确的.
The compiler is correct to do that since off_t is not conforming to the standard.
这让我感到困惑,因为我想要c ++ 11作为nullptr& lambdas.我根本不在乎STRICT_ANSI.
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.
解决方案是使用-std = gnu ++ 11
The solution is to work with -std=gnu++11
(另一种选择是只对需要它的头文件进行typedef off_t typedef _off_t off_t;)
(Another option is to just typedef off_t for the header files that need it typedef _off_t off_t;)
这篇关于用C ++ 11编译时Mingw g ++无法识别off_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!