我写了尽可能小的测试问题:
#include <sys/types.h>
int main(int argc, char** argv) {
off_t l = 0;
return 0;
}
以下作品: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
任何人都可以解释为什么会发生这种情况?这让我困扰的原因是我试图在我的 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)
当使用选项 -std=c++11 时,编译器定义了 -D__STRICT_ANSI__ ,它删除了 off_t 的定义,仅保留 _off_t 定义。
编译器这样做是正确的,因为 off_t 不符合标准。
这让我很困惑,因为我想要 c++11 来处理像 nullptr 和 lambdas 这样很酷的东西。我根本不关心STRICT_ANSI。
解决方案是使用 -std=gnu++11
(另一种选择是只为需要它的头文件输入 typedef off_t typedef _off_t off_t;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)