设为首页 加入收藏

TOP

持续构建:C++容器化编译环境
2019-09-24 18:11:00 】 浏览:175
Tags:持续 构建 容器 编译 环境

gcc官方镜像


在Docker Hub上提供的gcc Docker 官方镜像如下所示,通过在enable-languages的配置选项中设定c++,从而使gcc镜像也具有编译c++代码的能力。


在这里插入图片描述


使用docker pull命令获取当前最新版本的9.2.0版


[root@host132 ~]# docker pull gcc:9.2.0
9.2.0: Pulling from library/gcc
4a56a430b2ba: Pull complete
4b5cacb629f5: Pull complete
14408c8d4f9a: Pull complete
ea67eaa7dd42: Pull complete
4d134ac3fe4b: Pull complete
dbc65b875791: Pull complete
53308bd32679: Pull complete
da5ff526afd1: Pull complete
7704e65e7dab: Pull complete
Digest: sha256:c0f4919207ad6d73dad9f98e532f1cb224159e9c7d0a257665564526ae85bf7f
Status: Downloaded newer image for gcc:9.2.0
[root@host132 ~]# docker images |grep gcc
gcc                                    9.2.0              201020b8c956        13 hours ago        1.14GB
[root@host132 ~]#


gcc版本确认


使用g++ -v确认当前构建用的g++编译器的版本信息,具体执行示例如下所示


[root@host132 g++]# docker run --rm -it gcc:9.2.0 g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-linux-gnu/9.2.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: /usr/src/gcc/configure --build=x86_64-linux-gnu --disable-multilib --enable-languages=c,c++,fortran,go
Thread model: posix
gcc version 9.2.0 (GCC)
[root@host132 g++]#


事前准备


准备如下编译用的c++demo代码


[root@host132 g++]# cat demo/src/main.c
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello liumiaocn" << endl;
    return 0;
}


[root@host132 g++]#


使用g++构建&链接


c++语言编译也分为编译和链接两个阶段,这里为了简单演示,一步直接生成可执行文件。具体执行示例命令如下所示:


[root@host132 g++]# docker run --rm -v $(pwd)/demo/src/:/demo/src -it gcc:9.2.0 g++ -o /demo/src/demo /demo/src/main.c
[root@host132 g++]#


确认编译和链接所生成的结果文件:


[root@host132 g++]# ls demo/src/demo
demo/src/demo
[root@host132 g++]# file demo/src/demo
demo/src/demo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.2.0, not stripped
[root@host132 g++]#


确认结果


由于生成的为可执行文件,可以通过容器的方式确认结果输出
[root@host132 g++]# docker run --rm -v $(pwd)/demo/src/:/demo/src -it gcc:9.2.0 /demo/src/demo
Hello liumiaocn
[root@host132 g++]#


[root@host132 g++]# ./demo/src/demo
Hello liumiaocn
[root@host132 g++]#


总结


本文只是一个极为简单的示例,不具有任何实际的意义,在稍微复杂一点的项目之中,更为重要的是依赖的管理,以及Makefile的实践的指导相结合才是C++语言的项目在持续集成中需要重点注意的内容。


参考内容
https://hub.docker.com/_/gcc


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于 Linux五种IO模型 下一篇C++类中的函数重载实例

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目