簡體   English   中英

Linux Docker 中的 CMake 庫缺少依賴項

[英]CMake Lib in Linux Docker missing dependencies

I use CMake to create the DLL and SO file from my own C++ library, which I then call in my C# code via DLLImport. 到目前為止,這在 Windows 和 Linux (Docker) 下有效。 現在該庫已經擴展,它繼續在 Windows 和 DLL 上工作。 但是,在 Linux 下,我現在在調用 DLL function 時收到以下錯誤消息:

無法加載共享庫“CustomLib”或其依賴項之一。 為了幫助診斷加載問題,請考慮設置 LD_DEBUG 環境變量: libCustomLib:無法打開共享 object 文件:沒有此類文件或目錄

由於它之前在 Linux 下工作過,我認為它應該找到庫,因此依賴關系是問題所在。 但現在我不知道如何進行。

一位同事幫助我進行了分析,我得到了以下信息

root@f266455d4988:/app# LD_DEBUG=all /app/libCustomLib.so
Segmentation fault

root@f266455d4988:/app# ldd ./libCustomLib.so
        linux-vdso.so.1 (0x00007ffc9d983000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f05720a5000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f057208b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0571eca000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0571d47000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f05722d8000)
root@f266455d4988:/app#

我正在使用 .NET 5.0 和 GCC 編譯器

CMake 配置

      "name": "Linux-GCC-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "cmakeExecutable": "cmake",
      "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "inheritEnvironments": [ "linux_x64" ],
      "remoteMachineName": "${defaultRemoteMachineName}",
      "remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/src",
      "remoteBuildRoot": "$HOME/${projectDirName}/${name}",
      "remoteInstallRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/install/${name}",
      "remoteCopySources": true,
      "rsyncCommandArgs": "-t --delete --delete-excluded",
      "remoteCopyBuildOutput": false,
      "remoteCopySourcesMethod": "rsync",
      "addressSanitizerRuntimeFlags": "detect_leaks=0"

CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

include_directories(${CMAKE_SOURCE_DIR}/CustomLib/xyz)
include_directories(${CMAKE_SOURCE_DIR}/CustomLib/xxx)
link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

file(GLOB headers *.h)
file(GLOB headers *.hpp)

add_library (CustomLib SHARED CustomLib.cpp 
                ...
                ${headers})

add_executable (CustomLibExe CustomLib.cpp                
                ${headers})

target_compile_features(CustomLib PUBLIC cxx_std_17)
target_compile_features(CustomLibExe PUBLIC cxx_std_17)

target_compile_options (CustomLib PUBLIC -fexceptions)
target_compile_options (CustomLibExe PUBLIC -fexceptions)

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["testAPI/testAPI.csproj", "testAPI/"]
RUN dotnet restore "testAPI/testAPI.csproj"
COPY . .
WORKDIR "/src/testAPI"
RUN dotnet build "testAPI.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "testAPI.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "testAPI.dll"]

docker-compose的api部分

test_api:
    image: ${DOCKER_REGISTRY-}testapi
    container_name: testapi
    build:
      context: .
      dockerfile: testAPI/Dockerfile

    networks:
      - test_network

    depends_on:
     - test_sql

    environment:
     LD_LIBRARY_PATH: "/app"
     #LD_LIBRARY_PATH: "/lib/x86_64-linux-gnu"

    ports:
     - "5000:80"

我們已經解決了這個問題。 原因是后端運行的 Linux 發行版 (Debian) 與構建 DLL/SO 的 Linux (Ubuntu) 不同。

我將 Ubuntu 的 dockerfile 中的圖像從 ...:5.0 更改為 ...:5.0-focal。

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM