简体   繁体   English

用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;
}

The following works: g++ test.cpp 以下工作: g++ test.cpp

But If I try to compile with c++11 I get: 但是,如果我尝试使用c ++ 11进行编译,则会得到:

c:\\test>g++ -std=c++11 test.cpp 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. 这困扰我的原因是我试图在我的c ++ 11代码中使用zlib库。 The library zlib.h uses off_t a lot. 库zlib.h经常使用off_t。

My compiler version is: gcc 4.7.2 我的编译器版本是: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)

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. 当使用选项-std = c ++ 11时,编译器定义了-D__STRICT_ANSI__,它删除了off_t的定义,仅保留了_off_t的定义。

The compiler is correct to do that since off_t is not conforming to the standard. 编译器这样做是正确的,因为off_t不符合标准。

This was confusing for me cause I wanted c++11 for the cool stuff like nullptr & lambdas. 这让我感到困惑,因为我想要c ++ 11作为诸如nullptr和lambdas之类的酷东西。 I didn't care about STRICT_ANSI at all. 我根本不在乎STRICT_ANSI。

The solution is to work with -std=gnu++11 解决方案是使用-std = gnu ++ 11

(Another option is to just typedef off_t for the header files that need it typedef _off_t off_t;) (另一种选择是只对需要它的头文件使用typedef off_t typedef _off_t off_t;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM