簡體   English   中英

C++/Cmake - /usr/bin/ld: 找不到 -lsentry

[英]C++/Cmake - /usr/bin/ld: cannot find -lsentry

我正在嘗試在我的 C++ 項目中安裝哨兵庫。

是他們如何安裝它的文檔的鏈接。

這是我構建他們的庫后生成的文件:

在此處輸入圖像描述

我在我的項目中創建了一個名為dep的文件夾,用於保存依賴項。 由於我想將哨兵庫添加為依賴項,因此我在其中創建了一個文件夾:

在此處輸入圖像描述

這是哨兵庫 CMakeLists.txt 的內容:

add_library(sentry SHARED IMPORTED )
set_target_properties(sentry PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/lib/libsentry.so )

這是我嘗試將sentry庫鏈接到我的主項目的方式:

target_include_directories(${AppName} PUBLIC ../../dep/sentry)

target_link_libraries(${AppName} PUBLIC
        Common
        asio
        sentry
        ${Boost_FILESYSTEM_LIBRARY}
        ${Boost_SYSTEM_LIBRARY}
        gRPC::grpc++
        gRPC::grpc++_reflection
        )

當我嘗試構建我的項目時,我收到此錯誤:

[ 52%] Linking CXX executable ../../bin/WorldServer
/usr/bin/ld: cannot find -lsentry
collect2: error: ld returned 1 exit status
make[3]: *** [Source/WorldServer/CMakeFiles/WorldServer.dir/build.make:1499: bin/WorldServer] Error 1
make[2]: *** [CMakeFiles/Makefile2:632: Source/WorldServer/CMakeFiles/WorldServer.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:639: Source/WorldServer/CMakeFiles/WorldServer.dir/rule] Error 2
make: *** [Makefile:203: WorldServer] Error 2

知道如何解決此錯誤嗎?

默認情況下,導入庫的定義是其CMakeLists.txt及其后代的本地定義。 您在sentry/CMakeLists.txt中定義目標sentry ,因此您的主要CMakeLists.txt看不到它。

以下任何一項都可以解決問題:

  • 為全局定義sentry添加 GLOBAL 關鍵字:

     add_library(sentry SHARED IMPORTED GLOBAL)
  • sentry目標的定義移動到主CMakeLists.txt中。

  • 而不是sentry/CMakeLists.txt創建任何.cmake文件並通過include()命令包含它。


實際上, Sentry 項目提供了現成的腳本來使用 CMake 中的庫。 假設在sentry/目錄下你已經完成了庫的安裝,你可以使用find_package創建 IMPORTED 目標:

# in main CMakeLists.txt

# Among other things, this creates sentry::sentry IMPORTED target.
find_package(sentry REQUIRED PATHS ${CMAKE_CURRENT_SOURCE_DIR}/sentry)
...
target_link_libraries(${AppName} PUBLIC
  ...
  sentry::sentry # Use IMPORTED target for link with the library.
  ...
)

暫無
暫無

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

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