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.

  • clang++ should also come with the necessary headers to compile c++. But see stackoverflow.com/questions/22522933/… – planetmaker Jan 9 '20 at 9: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 9 '20 at 9:55

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:


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.