3

I'm trying to build a small alpine-based docker container to build c++ app using clang++. Dockerfile looks like this:

FROM alpine:3.10
RUN apk add --no-cache clang

But when I'm trying to compile a hello world I'm getting the following error:

$ printf '#include <iostream>\nint main(){std::cout<<"Hello"<<std::endl;}' > test.cxx
$ clang++ test.cxx -o test
test.cxx:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.

However, if I'll add a g++ package to this container, this example compiles and works as expected.

It seems that g++ package contains all the c++ headers:

apk info --who-owns /usr/include/c++/9.2.0/iostream 
/usr/include/c++/9.2.0/iostream is owned by g++-9.2.0-r3

Is there any way to install c++ headers without installing g++? I'd like to keep my docker image small, and g++ with its dependencies is quite heavy.

Denis Sheremet
  • 193
  • 1
  • 5
  • clang++ should also come with the necessary headers to compile c++. But see https://stackoverflow.com/questions/22522933/where-are-clang-c11-header-files – planetmaker Jan 09 '20 at 09:52
  • @planetmaker I've seen mentions of `libc++` package, but there's no such in alpine repo, and `clang` installs `libstdc++` as it's dependency – Denis Sheremet Jan 09 '20 at 09:55

1 Answers1

1

Unfortunately, g++ is the only package providing iostream and the standard C++ headers. You can confirm this by searching the Alpine Linux package index by contents:

https://pkgs.alpinelinux.org/contents?file=iostream&path=&name=&branch=v3.10&arch=x86_64

However, for keeping your image small, you could install the g++ package and just remove all g++ executables from your final image, either by using a two-stage docker build or by sqashing the image after built, as described in: Lightweight GCC for Alpine.

Essentially, you should keep everything under /usr/include/c++ and /usr/lib. You can delete the gcc executable binaries under /usr/bin, as well as cc1plus under /usr/libexec/gcc.

valiano
  • 629
  • 4
  • 16