Centos6 gcc6:编译简单的c ++ 11测试文件

时间:2017-07-17 23:55:58

标签: c++ c++11 gcc abi gcc6

我安装了devtoolset-6的centos 6的docker镜像,我想用gcc>=5中提供的新ABI构建我的代码,在本例中为gcc6。不知怎的,我没有得到我希望默认工作的东西。我尝试了各种各样的变化,但我不知道我错过了什么微不足道的事情。

任何有关我缺少的建议都将不胜感激......

在编译下面的示例代码时,我希望看到:

$ gcc -c test.cpp –o test.o
$ nm test.o
0000000000000000 T _Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
0000000000000000 r _ZStL19piecewise_construct

构建以下Dockerfile会使用gcc6:

生成centos6图像
FROM        centos:6
MAINTAINER  aap.noot@gmail.com

RUN yum -y update \
    && yum clean all \
    && yum -y install make \
    && yum clean all

RUN yum -y install centos-release-scl \
    && yum -y install devtoolset-6-gcc devtoolset-6-binutils devtoolset-6-gcc-c++ \
    && yum clean all \
    && echo 'source /opt/rh/devtoolset-6/enable' >> ~/.bashrc

RUN /usr/bin/scl enable devtoolset-6 true
ENTRYPOINT ["/usr/bin/scl", "enable", "devtoolset-6", "--"]
CMD ["/usr/bin/scl", "enable", "devtoolset-6", "--", "/bin/bash"]

并使用

运行图像
docker run -it centos6_gcc6_test

以下代码段会给出预期结果

g++ --version
echo '#include <string>' > test.cpp
echo 'void f(std::string s) {}' >> test.cpp
cat test.cpp
g++ -c -o test.o test.cpp
nm test.o | c++filt

我得到旧的ABI版本,即使gcc文档声明默认是生成new ABI

[root@3b58135d30a2 /]# g++ --version
g++ (GCC) 6.2.1 20160916 (Red Hat 6.2.1-3)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@3b58135d30a2 /]# echo '#include <string>' > test.cpp
[root@3b58135d30a2 /]# echo 'void f(std::string s) {}' >> test.cpp
[root@3b58135d30a2 /]# cat test.cpp
#include <string>
void f(std::string s) {}
[root@3b58135d30a2 /]# g++ -c -o test.o test.cpp
[root@3b58135d30a2 /]# nm test.o | c++filt
0000000000000000 T f(std::basic_string<char, std::char_traits<char>,   std::allocator<char> >)
0000000000000000 r std::piecewise_construct
[root@3b58135d30a2 /]#

即使使用-D_GLIBCXX_USE_CXX11_ABI=1明确编译,它仍然会给出错误的结果:

[root@3b58135d30a2 /]# g++ -D_GLIBCXX_USE_CXX11_ABI=1 -c -o test.o test.cpp
[root@3b58135d30a2 /]# nm test.o | c++filt
0000000000000000 T f(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
0000000000000000 r std::piecewise_construct

显然,我错过了一些微不足道的事情。

任何帮助?

1 个答案:

答案 0 :(得分:2)

问题是devtoolset-6中的gcc6编译器没有像Praetorian建议的那样拥有正确的stdlib。

从头开始编译gcc6编译器解决了这个问题。

相关问题