Search code examples
bashcmakecompilationstatic-librariescross-compiling

Is this a static build?


Firstly, forgive me for my ignorance on this topic - I'm new to compiling as a whole and far from a programmer, and I'm trying to understand the concept of static vs shared builds/libraries.

I've adapted the guide here to cross-compile the x265 project from source using a MinGW-W64 toolchain to run on Windows. I'm trying to make it a static build, but when I do:

cd /ffmpeg_sources && if /cd x265 2> /dev/null; then hg pull && hg update && cd ..; else hg clone https://bitbucket.org/multicoreware/x265; fi &&
cd x265/build/linux && PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" \
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/usr/local" -DENABLE_SHARED=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_EXE_LINKER_FLAGS="-static" ../../source \
-DCMAKE_TOOLCHAIN_FILE="/ffmpeg_sources/x265/build/msys/toolchain-x86_64-w64-mingw32.cmake" &&
make -j$(nproc) &&
make install

...it creates the resulting files:

-- Installing: /usr/local/lib/libx265.a
-- Installing: /usr/local/include/x265.h
-- Installing: /usr/local/include/x265_config.h
-- Installing: /usr/local/lib/pkgconfig/x265.pc
-- Installing: /usr/local/bin/x265.exe

Aside from the .exe file, there are at least three other files included as part of the installation, two of which are libraries, and I'm assuming they're there because they're relied on by the executable. However, it's always been my impression that "static" = portable, which having a few less files doesn't seem to satisfy.

Is this what is intended when creating a static build - that instead of relying on .so or .dll files, it will rely on just the .a and .h files? Or am I simply misunderstanding the Cmake switches I'm using and can this build be made even more static?


Solution

  • As its landing page says:

    x265 is a H.265 / HEVC video encoder application library, designed to encode video or images into an H.265 / HEVC encoded bitstream.

    the x265 project primarily provides a library that developers can link with their applications to provide them with x265, H.265/HEVC encoding functionality. Secondarily, it provides a commandline tool, x265 for H.265/HEVC encoding of an input file to an output file.

    One would therefore expect the package installation to provide -

    The library

    -- Installing: /usr/local/lib/libx265.a

    Check.

    One or more header files enabling compilation of library clients

    -- Installing: /usr/local/include/x265.h

    -- Installing: /usr/local/include/x265_config.h

    Check.

    A pkg-config file to provide developers with the package's compilation and linkage metadata.

    -- Installing: /usr/local/lib/pkgconfig/x265.pc

    Check.

    The commandline tool

    -- Installing: /usr/local/bin/x265.exe

    Check.

    It would also be usual for a library package to offer a configuration switch to select building of a shared/dynamic library - libname.so (Windows: [lib]name.dll) - or a static library - libname.a (Windows: [lib]name.(a|lib)) - defaulting to shared.

    You have opted for a static build:

    DENABLE_SHARED=OFF
    

    and have accordingly got just the static library libx265.a. Your commandline executable x265.exe has a buildtime dependency on that library and its header files - just as would any other application that requires linkage with libx265. But since it has been linked with the static library libx265.a, the executable, once built, physically incorporates all the parts of libx265.a that it depends upon and has no runtime dependency on it; indeed, runtime dependency on a static library is categorically impossible.

    Furthermore, since you specified:

    -DCMAKE_EXE_LINKER_FLAGS="-static"
    

    The linkage of your executable was fully static, i.e. the linker will have been obliged to find and link a static, not shared, version of every library required by the linkage, not just libx265, and on the evidence posted it succeeded.

    Bottom line: Your installation appears to have gone exactly as it should, and you have a fully static executable x265.exe.