gcc 4.8.1:将c代码与c ++ 11代码相结合

lrl*_*eon 5 c gcc c++11

我没有花太多精力去发现原因,但是gcc 4.8.1给我编译很多麻烦来编译c和c ++以及c ++ 11中的一些新东西的旧源代码.

我设法隔离了这段代码中的问题:

# include <argp.h>
# include <algorithm>
Run Code Online (Sandbox Code Playgroud)

它与g++ -std=c++0x -c -o test-temp.o test-temp.C版本4.6.3,ubuntu 12.04 编译良好

相比之下,对于4.8.1版本,相同的命令行会引发很多错误:

In file included from /home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/x86intrin.h:30:0,
                 from /home/lrleon/GCC/include/c++/4.8.1/bits/opt_random.h:33,
                 from /home/lrleon/GCC/include/c++/4.8.1/random:51,
                 from /home/lrleon/GCC/include/c++/4.8.1/bits/stl_algo.h:65,
                 from /home/lrleon/GCC/include/c++/4.8.1/algorithm:62,
                 from test-temp.C:4:
/home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/mmintrin.h: In function ‘__m64 _mm_cvtsi32_si64(int)’:
/home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/mmintrin.h:61:54: error: can’t convert between vector values of different size
   return (__m64) __builtin_ia32_vec_init_v2si (__i, 0);
                                                      ^
Run Code Online (Sandbox Code Playgroud)

... 以及更多.

如果我执行,也会发生同样的事

g++ -std=c++11 -c -o test-temp.o test-temp.C; 再次,版本4.8.1

但是,如果我交换标题行,那就是

# include <algorithm>
# include <argp.h>
Run Code Online (Sandbox Code Playgroud)

然后所有编译都很好.

有人启发我了解发生了什么?

neu*_*uro 3

我遇到了同样的问题。因为它真的很烦人,所以我把它砍下来了<argp.h>

这是在 ubuntu 14.04 / gcc 4.8.2 上触发错误的代码(在标准 gcc 标头 argp.h 中):

/* This feature is available in gcc versions 2.5 and later.  */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
#  define __attribute__(Spec) /* empty */
# endif
Run Code Online (Sandbox Code Playgroud)

这可能是为了使标头与旧的 gcc 和严格的 ANSI C++ 定义兼容。问题是 --std=c++11 设置__STRICT_ANSI__宏。

我已经评论过#define __attribute__(spec)并且编译工作正常!

由于注释系统标头是不切实际的,解决方法是使用g++ --std=gnu++11而不是g++ --std=c++11因为它没有定义__STRICT_ANSI__. 它对我来说有效。

这似乎是 gcc 中的一个错误。