/usr/include/bits/errno.h:26:11: fatal error: linux/errno.h: No such file or directory # include <linux/errno.h>

I use the below centos8

roboxes/centos8:latest 

sh-4.4# cat /etc/redhat-release
CentOS Linux release 8.5.2111

but when I use the g++ compile my C++ program:

 sh-4.4#  g++ ./hello.cpp -o hello_cpp

I get error:

In file included from /usr/include/errno.h:28,
                 from /usr/include/c++/8/cerrno:42,
                 from /usr/include/c++/8/ext/string_conversions.h:44,
                 from /usr/include/c++/8/bits/basic_string.h:6404,
                 from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from ./hello.cpp:1:
/usr/include/bits/errno.h:26:11: fatal error: linux/errno.h: No such file or directory
 # include <linux/errno.h>
           ^~~~~~~~~~~~~~~
compilation terminated.

my content of hello.cpp:

sh-4.4# cat hello.cpp 
#include <iostream>
using namespace std;

int main() {
  cout<<"hello cpp."<<endl;

  cout<<"2.xxx"<<endl;

  cout<<"3.yyy"<<endl;

  return 0; 
}

You need the linux header files, but I don’t know how you can install them properly in a centos container. If you search for the error message you will find suggestions to mount the header files from the host. I tried and it didn’t work. Kernels are installed on the host, but the header source files could be in the container too. Another suggestion is to install “glibc-devel”. That didn’t work for me either.

Update:

If you just need a container to build C++ sourcecode you can use the official gcc image.

If you want to use g++ in a Dockerfile, you can use multi-stage builds and copy the result to the final image.