使用 Cmake 链接库 - 不直接使用

Linking libraries using Cmake - not used directly

我是 Cmake 新手。我正在使用 Ubuntu 20.

我在本地构建并安装了 dlt-daemon-2.18.8 (/home/map/third_party) 并构建了日志库。

日志库内容如下CMakeLists.txt

find_package(PkgConfig REQUIRED)
pkg_search_module(DLT automotive-dlt REQUIRED)
message("*****   pkg_search_module DLT_MODULE_NAME = ${DLT_MODULE_NAME} *****")
message("*****   pkg_search_module DLT_FOUND = ${DLT_FOUND} *****")
message("*****   pkg_search_module DLT_LIBRARIES = ${DLT_LIBRARIES} *****")
message("*****   pkg_search_module DLT_LINK_LIBRARIES = ${DLT_LINK_LIBRARIES} *****")
message("*****   pkg_search_module DLT_LIBRARY_DIRS = ${DLT_LIBRARY_DIRS} *****")
message("*****   pkg_search_module DLT_LDFLAGS = ${DLT_LDFLAGS} *****")
message("*****   pkg_search_module DLT_LDFLAGS_OTHER = ${DLT_LDFLAGS_OTHER} *****")
message("*****   pkg_search_module DLT_INCLUDE_DIRS = ${DLT_INCLUDE_DIRS} *****")
add_subdirectory(src)

以下是CMakeLists.txt在日志文件夹(主要CMake文件)中的src文件夹中的部分

target_link_libraries(${PROJECT_NAME}
    PUBLIC
        ${DLT_LIBRARIES}
        ara::core-types
        apd::Threads
)

以下是 Log 库的输出 CMakeLists.txt & 它看起来没问题。

-- Checking for one of the modules 'automotive-dlt'
*****   pkg_search_module DLT_MODULE_NAME = automotive-dlt *****
*****   pkg_search_module DLT_FOUND = 1 *****
*****   pkg_search_module DLT_LIBRARIES = dlt;rt;pthread *****
*****   pkg_search_module DLT_LINK_LIBRARIES = /home/map/third_party/lib/libdlt.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so *****
*****   pkg_search_module DLT_LIBRARY_DIRS = /home/map/third_party/lib *****
*****   pkg_search_module DLT_LDFLAGS = -L/home/map/third_party/lib;-ldlt;-lrt;-lpthread *****
*****   pkg_search_module DLT_LDFLAGS_OTHER =  *****
*****   pkg_search_module DLT_INCLUDE_DIRS = /home/map/third_party/include/dlt;/home/map/third_party/include *****
-- Configuring done

在此之后,我正在使用上述日志库构建 1 个应用程序和 2 个库,但它无法 link 并出现以下错误

[100%] Linking CXX shared library libe2exf.so
/usr/bin/ld: cannot find -ldlt

[100%] Linking CXX shared library libe2e.so
/usr/bin/ld: cannot find -ldlt

[100%] Linking CXX executable execution-manager
/usr/bin/ld: cannot find -ldlt

如果我安装这两个组件(sudo apt-get install libdlt2 和 sudo apt-get install libdlt-dev),那么 2 个库(libe2exf.so 和 libe2e.so)正在构建但应用程序失败(执行管理器)出现以下错误 /usr/bin/ld: /home/map/third_party/lib/libara_log.a(log_stream.cpp.o): 在函数中 ara::log::LogStream::operator<<(ara::core::Span<ara::core::internal::Byte const, 18446744073709551615ul>)':log_stream.cpp:(.text+0xbaf): undefined reference to dlt_user_log_write_sized_utf8_string' /usr/bin/ld: /home/map/third_party/lib/libara_log.a(log_stream.cpp.o): 在函数中 ara::log::LogStream::WithLocation(ara::core::internal::basic_string_view<char, std::char_traits<char> >, int)':log_stream.cpp:(.text+0xdc6): undefined reference to dlt_user_log_write_sized_utf8_string'

我不想安装 libdlt2 和 libdlt-dev,请让我知道如何 link 上面的 1 个应用程序和 2 个库到 /home/map/third_party/lib/libdlt.so;

提前致谢您好,

我在本地构建并安装了 dlt-daemon-2.18.8 (/home/map/third_party) 并构建了日志库。

日志库内容如下CMakeLists.txt

find_package(PkgConfig REQUIRED)
pkg_search_module(DLT automotive-dlt REQUIRED)
message("*****   pkg_search_module DLT_MODULE_NAME = ${DLT_MODULE_NAME} *****")
message("*****   pkg_search_module DLT_FOUND = ${DLT_FOUND} *****")
message("*****   pkg_search_module DLT_LIBRARIES = ${DLT_LIBRARIES} *****")
message("*****   pkg_search_module DLT_LINK_LIBRARIES = ${DLT_LINK_LIBRARIES} *****")
message("*****   pkg_search_module DLT_LIBRARY_DIRS = ${DLT_LIBRARY_DIRS} *****")
message("*****   pkg_search_module DLT_LDFLAGS = ${DLT_LDFLAGS} *****")
message("*****   pkg_search_module DLT_LDFLAGS_OTHER = ${DLT_LDFLAGS_OTHER} *****")
message("*****   pkg_search_module DLT_INCLUDE_DIRS = ${DLT_INCLUDE_DIRS} *****")

以下是 Log 库的输出 CMakeLists.txt & 它看起来没问题。

-- Checking for one of the modules 'automotive-dlt'
*****   pkg_search_module DLT_MODULE_NAME = automotive-dlt *****
*****   pkg_search_module DLT_FOUND = 1 *****
*****   pkg_search_module DLT_LIBRARIES = dlt;rt;pthread *****
*****   pkg_search_module DLT_LINK_LIBRARIES = /home/map/third_party/lib/libdlt.so;/usr/lib/x86_64-linux-gnu/librt.so;/usr/lib/x86_64-linux-gnu/libpthread.so *****
*****   pkg_search_module DLT_LIBRARY_DIRS = /home/map/third_party/lib *****
*****   pkg_search_module DLT_LDFLAGS = -L/home/map/third_party/lib;-ldlt;-lrt;-lpthread *****
*****   pkg_search_module DLT_LDFLAGS_OTHER =  *****
*****   pkg_search_module DLT_INCLUDE_DIRS = /home/map/third_party/include/dlt;/home/map/third_party/include *****
-- Configuring done

在此之后,我正在使用上述日志库构建 1 个应用程序和 2 个库,但它无法 link 并出现以下错误

[100%] Linking CXX shared library libe2exf.so
/usr/bin/ld: cannot find -ldlt

[100%] Linking CXX shared library libe2e.so
/usr/bin/ld: cannot find -ldlt

[100%] Linking CXX executable execution-manager
/usr/bin/ld: cannot find -ldlt

如果我安装这两个组件(sudo apt-get install libdlt2 和 sudo apt-get install libdlt-dev),那么 2 个库(libe2exf.so 和 libe2e.so)正在构建但应用程序失败(执行管理器)出现以下错误

/usr/bin/ld: /home/map/third_party/lib/libara_log.a(log_stream.cpp.o): in function ara::log::LogStream::operator<<(ara::core::Span<ara::core::internal::Byte const, 18446744073709551615ul>)':log_stream.cpp:(.text+0xbaf): undefined reference to dlt_user_log_write_sized_utf8_string'

/usr/bin/ld: /home/map/third_party/lib/libara_log.a(log_stream.cpp.o): in function ara::log::LogStream::WithLocation(ara::core::internal::basic_string_view<char, std::char_traits<char> >, int)':log_stream.cpp:(.text+0xdc6): undefined reference to dlt_user_log_write_sized_utf8_string'

我不想安装 libdlt2 和 libdlt-dev,请让我知道如何 link 上面的 1 个应用程序和 2 个库到 /home/map/third_party/lib/libdlt.so;

我正在使用以下 cmake 命令进行构建。如有不妥请告知

cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DARA_ENABLE_TESTS=OFF \
-DIMPORTED_LOCATION=/home/manoj/third_party,/home/manoj/third_party/lib/pkgconfig \
-DINTERFACE_INCLUDE_DIRECTORIES=/home/manoj/third_party/include \
-DINCLUDE_DIRECTORIES=/home/manoj/third_party \
-DCMAKE_MODULE_PATH=/home/manoj/third_party/share/cmake-3.16/Modules \
-DCMAKE_PREFIX_PATH=/home/manoj/third_party \
-DCMAKE_INSTALL_PREFIX=/home/manoj/third_party \
-DCMAKE_BUILD_TYPE=Release ..

提前致谢

据我所知,您只需将 link 目录添加到您的目标:

target_link_directories(${PROJECT_NAME}
    PUBLIC
        ${DLT_LIBRARY_DIRS}
)

这是因为DLT_LIBRARIES不包含有关这些库的路径信息。路径信息包含在目录变量中。

您可以在 DLT_LDFLAGS 中看到 linking 需要在 linking 到库之前添加目录。

一个更好的解决方案是使用 DLT 的导入目标(如果它导出一个目标),因为它会自己处理所有这些。