我使用 CMake 從我自己的 C 庫中創建 DLL 和 SO 檔案,然后我通過 DLLImport 在我的 C# 代碼中呼叫它們。到目前為止,這在 Windows 和 Linux (Docker) 下都有效。現在該庫已經擴展,它可以繼續在帶有 DLL 的 Windows 上作業。但是,在 Linux 下,我現在在呼叫 DLL 函式時收到以下錯誤訊息:
無法加載共享庫“CustomLib”或其依賴項之一。為了幫助診斷加載問題,請考慮設定 LD_DEBUG 環境變數: libCustomLib:無法打開共享物件檔案:沒有這樣的檔案或目錄
由于它以前在 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"
uj5u.com熱心網友回復:
我們已經解決了這個問題。原因是后端運行的 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422434.html
標籤:
上一篇:輸入字串的格式不正確。網
下一篇:HTTP客戶端方法空例外
