drogon icon indicating copy to clipboard operation
drogon copied to clipboard

How to compile a project fully statically?

Open mpv945 opened this issue 2 years ago • 6 comments

How to achieve full static compilation of the project? The compiled executable file can be run in binary mode on other linux systems. (The operating environment does not need to install related dependencies)

mpv945 avatar Apr 19 '22 08:04 mpv945

You have to use the static libraries of all the dependencies (trantor, openssl, zlib, libpq and so on) and build drogon in static mode, even so, you need to ensure that the build system and the target system's lower-level libraries (for example, libc, libstdc++) are compatible, In conclusion, this is a difficult thing, so container technologies like docker are becoming more and more popular.

an-tao avatar Apr 19 '22 09:04 an-tao

For arm64 you might benefit from:

FROM omrmohamed/drogon:archlinux

I also recommend using Docker Slim

omarmohamedkh avatar Apr 22 '22 15:04 omarmohamedkh

您必须使用所有依赖项(trantor、openssl、zlib、libpq 等)的静态库并以静态模式构建 drogon,即使如此,您也需要确保构建系统和目标系统的低级库(比如libc、libstdc++)是兼容的,总而言之,这是一件很难的事情,所以像docker这样的容器技术越来越流行。

How to statically build the libraries that the project depends on, and try to ensure that the minimum dependencies are installed?

You have to use the static libraries of all the dependencies (trantor, openssl, zlib, libpq and so on) and build drogon in static mode, even so, you need to ensure that the build system and the target system's lower-level libraries (for example, libc, libstdc++) are compatible, In conclusion, this is a difficult thing, so container technologies like docker are becoming more and more popular.

How to statically build the libraries that the project depends on, and try to ensure that the minimum dependencies are installed?

mpv945 avatar Apr 26 '22 23:04 mpv945

For arm64 you might benefit from:

FROM omrmohamed/drogon:archlinux

I also recommend using Docker Slim

How can a statically compiled project run with the smallest volume capacity? Example: FROM scratch as runner?

mpv945 avatar Apr 26 '22 23:04 mpv945

I think you just:

FROM scratch
COPY static-binary /
ENTRYPOINT ["/static-binary"]

but I haven't compiled a drogon project statically before, although I would like to know the process if you managed to do this.

The other and easier way is to use a drogon base image with a compiler included and build with docker-slim. The image I mentioned is about 2.6GB for amd64 and 3GB for arm64 (Crypto++ is included) docker-slim produces 50MB images or 57MB if I want a basic shell in the container and of course this depends on the project code size.

omarmohamedkh avatar Apr 27 '22 01:04 omarmohamedkh

In case anyone is interested, the default instructions of drogon for installing the dependency packages will have some packages with both .so and .a files, and CMake prefers .so if both exist.

The list of shared libraries drogon will link against using packages:

  1. libjsoncpp.so - This one has no .a equivalent, you must build jsoncpp statically and install it with -DCMAKE_INSTALL_PREFIX:PATH=/usr
  2. libcares.so
  3. libuuid.so
  4. libbrotlidec.so
  5. libbrotlienc.so
  6. libbrotlicommon.so
  7. libz.so

These two may have also been linked, you can find out what shared libs are linked by opening the build/build.ninja file if you are building with Ninja, or makefiles if you use Unix Makefiles, and search for \.so or .so:

  1. libssl.so
  2. libcrypto.so

My process for forcing the use of static libs in linking, is to have a bash file that removes these .so files. You can play it safer and rename these files to something else until CMake finishes writing build instructions, then after that rename them back to .so. This allows CMake to only find .a libs and links against these.

An even safer method is to run CMake, then edit your build instructions file (build/build.ninja) and do a search and replace for .so -> .a.

Mis1eader-dev avatar Sep 18 '23 07:09 Mis1eader-dev