
CentOS 7がインストールされている地下イメージからドッカーイメージを構築しています。
コンパイルする必要があるコードにはC ++ 14/17標準の一部の機能が必要なため、デフォルトのgcc / g ++バージョンを4.8.5から親バージョンに更新する必要があります。
Dockerfileでg ++を更新するために、次のコマンドを実行するいくつかの記事や記事を読んでいます。
RUN yum -y install centos-release-scl && \
yum -y install devtoolset-7-gcc* && \
source scl_source enable devtoolset-7 &&
g++ -version
正しいバージョンが印刷されます。
g++(GCC) 7.3.1 20180303(Red Hat 7.3.1-5)
しかし、makeを介してコードをビルドすると、まだ古いバージョンを使用するため、-std=c++14
ビルドフラグを認識しません。これを確認するために、Makefileにバージョンターゲットを追加し、次のようにDockerfileでコマンドを実行しました。
ファイル生成:
# ...
CXX:=g++
FLAGS:=-Wall -fPIC -std=c++14
# ...
%.o: %.cpp
$(CXX) $(FLAGS) -c $< -o $@ $(INCLUDE_PATH)
# ...
version:
g++ -v
ドッカーファイル:
RUN cd /home/admin/${APP_NAME}/nginx-base/cplusplus && make version && make
Dockerのビルドフェーズ中の出力:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
g++ -Wall -fPIC -std=c++14 -c image_engine.cpp -o image_engine.o -I /opt/taobao/tengine/data/include
g++: error: unrecognized command line option '-std=c++14'
make: *** [image_engine.o] Error 1
The command '/bin/sh -c cd /home/admin/${APP_NAME}/nginx-base/cplusplus && make version && make' returned a non-zero code: 2
それでは、CentOSのデフォルトのg ++の代わりにMakefileでg ++ - 7をどのように有効にする必要がありますか?
答え1
Dockerに関するコメントと私の経験によれば、各行RUN
は別々のシェル環境で実行されるため、ある行に環境をインポートすると、RUN
その環境を別のコマンドで使用できなくなりますRUN
。
RUN source scl_source enable devtoolset-7 && cd /home/admin/${APP_NAME}/nginx-base/cplusplus && make version && make
前のコマンドの代わりにこの行を使用すると、そのコマンドRUN
に現在の環境が設定されますmake
。
答え2
回避策:
SHELL ["sh", "-c", "source scl_source enable $(scl -l) && sh -c \"$0\" \"$@\""]
これはすべての後続のRUNシェルを置き換えます。