库中的某些文件无法链接到 spdlog,但其他文件却可以

问题描述 投票:0回答:0

我遇到了一些相当奇怪的事情,我不知道如何弄清楚它的真相。 我正在使用

spdlog
作为库。我的代码分为两个库,一个名为
NativeUtils
,它是作为静态库构建的,另一个是名为
drivermonitor
的主库。这些是 Android JNI 库,是使用 Cmake 构建的。我包括我的 Cmakefiles。

我在

NativeUtils
中有一些源文件,我看到的是,在某些文件中,如果我调用像
spdlog::info
这样的 spdlog 函数,我会收到链接器错误,但在其他文件中则不会。

: && /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android26 --sysroot=/home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -fno-limit-debug-info  -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--gc-sections -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libdrivermonitor.so -o /home/kartik/Projects/DriverMonitor/service/build/intermediates/cxx/Debug/3n3s6c4k/obj/arm64-v8a/libdrivermonitor.so CMakeFiles/drivermonitor.dir/src/AiProcessor.cpp.o CMakeFiles/drivermonitor.dir/src/DriverMonitor.cpp.o CMakeFiles/drivermonitor.dir/src/DriverMonitorNative.cpp.o CMakeFiles/drivermonitor.dir/src/FrameCounter.cpp.o CMakeFiles/drivermonitor.dir/src/NetworkWrapper.cpp.o CMakeFiles/drivermonitor.dir/src/PreProcess.cpp.o CMakeFiles/drivermonitor.dir/test/NativePreProc.cpp.o  deps/NativeUtils/libNativeUtils.a  /home/kartik/Projects/DriverMonitor/deps/Snpe/lib/arm64-v8a/libSNPE.so  /home/kartik/Projects/DriverMonitor/deps/fastcv/lib/arm64-v8a/libfastcv.a  /home/kartik/Projects/DriverMonitor/deps/Snpe/lib/arm64-v8a/libc++_shared.so  _deps/fmt-build/libfmtd.a  _deps/spdlog-build/libspdlogd.a  /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/26/liblog.so  /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/26/libandroid.so  /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/26/libmediandk.so  /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/26/libjnigraphics.so  -pthread  -llog  -lc++_shared  /home/kartik/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/26/liblog.so  -latomic -lm && :

ld: error: undefined symbol: spdlog::details::log_msg::log_msg(spdlog::source_loc, fmt::v10::basic_string_view<char>, spdlog::level::level_enum, fmt::v10::basic_string_view<char>)
>>> referenced by logger.h:134 (_deps/spdlog-src/include/spdlog/logger.h:134)
>>>               AImageWrapper.cpp.o:(spdlog::logger::log(spdlog::source_loc, spdlog::level::level_enum, fmt::v10::basic_string_view<char>)) in archive deps/NativeUtils/libNativeUtils.a

这是我的 CMakeLists.txt 文件, 我的依赖项 CMakeLists.txt

cmake_minimum_required(VERSION 3.22)

project(SupportLibs)

find_library(native-media mediandk)
find_library(native-log log)
find_library(native-trace android)
find_library(native-graphics jnigraphics)
include(FetchContent)
FetchContent_Declare(fmt
        GIT_REPOSITORY https://github.com/fmtlib/fmt
        GIT_TAG 10.0.0)
FetchContent_Declare(spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog
        GIT_TAG v1.12.0)
FetchContent_MakeAvailable(fmt spdlog)

add_subdirectory(Snpe)
add_subdirectory(fastcv)
add_subdirectory(NativeUtils)

我使用

FetchContent
来获取
fmt
spdlog

这是 NativeUtils 的 CMakeLists.txt

cmake_minimum_required(VERSION 3.22)

project(NativeUtils)

file(GLOB sources src/*.cpp)
file(GLOB headers include/*.hpp)

add_library(NativeUtils STATIC "${sources}" "${headers}")
print_list("${sources}")
target_include_directories(NativeUtils PUBLIC inc)
target_link_libraries(NativeUtils PUBLIC snpe c++_shared)
target_link_libraries(NativeUtils PRIVATE ${native-log} fmt::fmt spdlog::spdlog fastcv)
target_compile_features(NativeUtils PRIVATE cxx_std_17)

我使用 GLOB 模式来获取源和标头。 最后是我的主库的 CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.22)
project(DriverMonitor CXX)

macro(print_list)
    foreach(arg ${ARGV})
        message("  ${arg}")
    endforeach()
endmacro()

set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../deps)

add_subdirectory(${distribution_DIR} ${CMAKE_CURRENT_BINARY_DIR}/deps)
file(GLOB headers inc/drivermonitor/*.h)
file(GLOB sources src/*.cpp test/*.cpp)
add_library(drivermonitor SHARED "${headers}" "${sources}")
target_compile_features(drivermonitor PRIVATE cxx_std_17)
target_include_directories(drivermonitor PRIVATE inc)
target_link_libraries(
        drivermonitor
        PRIVATE
        NativeUtils
        snpe
        fastcv
        c++shared
        fmt::fmt
        spdlog::spdlog
        ${native-log}
        ${native-trace}
        ${native-media}
        ${native-graphics}
)

我只是无法理解为什么在某些源文件中存在链接器错误,而在其他源文件中则没有,在同一个库中,所有文件都是使用同一组选项构建的。

android c++ cmake java-native-interface
© www.soinside.com 2019 - 2024. All rights reserved.