#Build with glibc, ship to musl

20 messages · Page 1 of 1 (latest)

sleek wharf
#

Hello 👋. I am struggle on building and shipping Cpp application. Currently I use cmake with static linking to avoid any dependency hell with Alpine Linux. Is what I am doing right? Also sometimes I got an error which says it can not link properly boost stack trace - it has double definition of some function.

Here is a related code
Dockerfile: https://github.com/tokKurumi/tim-messanger/blob/master/services/prompt.service/src/Dockerfile
CMakeLists: https://github.com/tokKurumi/tim-messanger/blob/master/services/prompt.service/src/CMakeLists.txt

Is what I am doing right?

GitHub

Contribute to tokKurumi/tim-messanger development by creating an account on GitHub.

GitHub

Contribute to tokKurumi/tim-messanger development by creating an account on GitHub.

coral hatchBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hard glen
#

What does the title mean? You need to compile and link the whole program with the same libc.

#

If you mean that you build statically against glibc, then ship to a system which only has musl, beware that you won't be able to use dlopen properly (and glibc will use it by default automatically for some features such as NSS)

sleek wharf
#

here is the exact problem in logs

------
 > [build 9/9] RUN --mount=type=cache,target=/root/.conan2 --mount=type=cache,target=/root/.cache/ccache     cmake -S . -B build     -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake     -DCMAKE_BUILD_TYPE=Release     -DCMAKE_CXX_COMPILER_LAUNCHER=ccache     -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc -static"     && cmake --build build --config Release:
0.455 -- Build files have been written to: /app/build
0.494 [ 33%] Building CXX object CMakeFiles/prompt.service.dir/main.cpp.o
0.812 [ 66%] Building CXX object CMakeFiles/prompt.service.dir/configuration/config.cpp.o
1.406 [100%] Linking CXX executable prompt.service
1.432 /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/libstdc++.a(eh_alloc.o): in function `__cxa_allocate_exception':
-1.432 (.text.__cxa_allocate_exception+0x0): multiple definition of `__cxa_allocate_exception'; /root/.conan2/p/b/boostd6b2addc3d65f/p/lib/libboost_stacktrace_from_exception.a(from_exception.o):from_exception.cpp:(.text+0x50): first defined here
1.550 collect2: error: ld returned 1 exit status
1.552 gmake[2]: *** [CMakeFiles/prompt.service.dir/build.make:163: prompt.service] Error 1
1.552 gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/prompt.service.dir/all] Error 2
1.552 gmake: *** [Makefile:91: all] Error 2
------
-ERROR: failed to build: failed to solve: process "/bin/sh -c cmake -S . -B build     -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake     -DCMAKE_BUILD_TYPE=Release     -DCMAKE_CXX_COMPILER_LAUNCHER=ccache     -DCMAKE_EXE_LINKER_FLAGS=\"-static-libstdc++ -static-libgcc -static\"     && cmake --build build --config Release" did not complete successfully: exit code: 2
#

let me clean the code and target to specific cmake + conan, since I didn't link static commit file:

[requires]
boost/1.89.0
libssh/0.11.2

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout
cmake_minimum_required(VERSION 3.10)

project(prompt.service LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++ -static-libgcc -static")

find_package(Boost REQUIRED)
find_package(libssh REQUIRED)

add_executable(prompt.service
    main.cpp
    configuration/config.cpp
)

target_include_directories(prompt.service PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

target_link_libraries(prompt.service PRIVATE
    boost::boost
    ssh::ssh
)

target_link_options(prompt.service PRIVATE
    -static-libstdc++
    -static-libgcc
    -static
)```

build command:
```sh
cmake -S . -B build \
    -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc -static" \
    && cmake --build build --config Release
sleek wharf
untold gale
# sleek wharf let me clean the code and target to specific cmake + conan, since I didn't link ...
cmake_minimum_required(VERSION 3.10)

project(prompt.service LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++ -static-libgcc -static")

find_package(Boost REQUIRED)
find_package(libssh REQUIRED)

add_executable(prompt.service
    main.cpp
    configuration/config.cpp
)

target_include_directories(prompt.service PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

target_link_libraries(prompt.service PRIVATE
    boost::boost
    ssh::ssh
)

# Why do you have this when you set the linking flags globally? It should include the linking already for it i think. Try omitting the section below and see if that works. 
target_link_options(prompt.service PRIVATE
    -static-libstdc++
    -static-libgcc
    -static
)```
#

Is this needed: set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++ -static-libgcc -static")

when this is already here: target_link_options(prompt.service PRIVATE -static-libstdc++ -static-libgcc -static )

#

@sleek wharf yeah I think that's fine actually. Do you have liboost installed dynamically?

sleek wharf
# untold gale Is this needed: `set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++ -static-libgcc -s...

moreover, in here

cmake -S . -B build \
    -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
+    -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc -static" \
    && cmake --build build --config Release

I also specify this behavior, just in a case. And I think this is not the problem. I tested what you suggested and got same error

sleek wharf
untold gale
sleek wharf
#

Yes I got it installed. I will be dropped out from installation in the find_package(Boost REQUIRED) line if I wouldn't

untold gale
#

If you mean that you build statically against glibc, then ship to a system which only has musl, so yeah. If you got liboost both static and installed on the system. That could be the issue

sleek wharf
# untold gale *If you mean that you build statically against glibc, then ship to a system whic...

So you think that system where I build (debian:bookworm-slim) has it is already installed and when I try to execute package manager I got the second definition - I got the error?
It could be possible, may be some of my debian apt packages use boost in these lines:

RUN apt update && apt install -y --no-install-recommends \
    build-essential \
    make \
    cmake \
    git \
    python3 \
    python3-venv \
    python3-pip \
    ccache \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# and after I do this, which could possible give me double definitions of boost

# Install Conan dependencies (with cached Conan home). Ensure profile exists inside cache mount.
RUN --mount=type=cache,target=/root/.conan2 \
    conan profile detect --force && \
    conan install . \
    --build=missing \
    -s compiler.libcxx=libstdc++11 \
    -g CMakeToolchain \
    -g CMakeDeps

I'll try to check this! Thx for the direction

#

may be the best way to me will be to use 2 build systems:

  1. for host with using libc (just for debugging)
  2. for container with musl (to shipping)

so I will no longer deal with static hell

untold gale
#

Either or, good luck

sleek wharf