Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 531
odedsh Avatar asked Oct 29 '13 18:10

odedsh


People also ask

How do I know if G ++ supports C ++ 11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

How do I enable C++ 11?

You need to add the option --std=c++11 (not c+11) to the compiler's command line, which tells the compiler to use the STanDard language version called C++11.


1 Answers

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;)

like image 145
odedsh Avatar answered Oct 12 '22 08:10

odedsh