在另一个项目中构建库时,构建系统找不到引用,为什么?

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

所以我有一个库,可以在使用其 CMake 自行编译时进行编译。然后我将此库作为 git 子模块包含在另一个项目中,但现在由于某种原因无法在代码中找到引用。

/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeGetResources'
/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeGetConnector'
/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeFreeConnector'

这是自己构建的库的CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(libfreedom LANGUAGES C)

# Specify the C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Add library target
add_library(libfreedom SHARED src/libfreedom.c)

target_include_directories(libfreedom PRIVATE /usr/include/libdrm)

# Prevent it from being called liblibfreedom.so
set_target_properties(libfreedom PROPERTIES
    OUTPUT_NAME "freedom"  # This will create libfreedom.so
)

# Install targets and configuration
install(TARGETS libfreedom DESTINATION lib)
install(FILES include/libfreedom.h DESTINATION include)

这是该项目的 CMakeLists.txt。请注意,

cmake
可以工作,但在尝试使用
make
ninja
时,我收到上面的错误消息。

cmake_minimum_required(VERSION 3.10)

project(Freedom)

# Add the submodule as a dependency
add_subdirectory(libfreedom)

# Create the executable
add_executable(Freedom src/main.c)

# Link the submodule library to the main project
target_link_libraries(Freedom PRIVATE libfreedom)

target_include_directories(Freedom PRIVATE libfreedom/include)
c cmake dependencies
1个回答
0
投票

尝试在

target_link_libraries(Freedom PRIVATE libfreedom)
中将 PRIVATE 更改为 PUBLIC。

我发现了这个: CMake 中的目标命令范围

© www.soinside.com 2019 - 2024. All rights reserved.