为使用C++20模块的库提供Config.cmake

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

我有一个用 C++20 模块编写的库。 我之前以

add_subdirectory(../NeLib ${CMAKE_CURRENT_BINARY_DIR}/NeLib)
的形式使用过这个库 现在我希望我提供一个NeLibConfig.cmake后,其他项目可以直接通过
find_package

使用它

项目结构为:

NeLib 文件.cppm 图像.cppm ...(其他一些库,都是cppm文件) CMakeLists.txt NeLibConfig.cmake.in

CMakeLists.txt文件内容为:

cmake_minimum_required(VERSION 3.29)
project(NeLib)

set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(VERSION "1.0.0")
set(CMAKE_CXX_STANDARD 23)
find_package(OpenCV REQUIRED)

add_library(NeLib STATIC )

file(GLOB module_files "*.cppm")

configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/NeLibConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/NeLibConfig.cmake"
    @ONLY
)
target_sources(NeLib
    PUBLIC
    FILE_SET cxx_modules TYPE CXX_MODULES FILES
    ${module_files}
)

install(TARGETS  ${PROJECT_NAME}  DESTINATION /usr/lib EXPORT ${PROJECT_NAME} FILE_SET cxx_modules)
install(EXPORT ${PROJECT_NAME} DESTINATION /usr/lib/cmake/NeLib/)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/NeLibConfig.cmake"
        DESTINATION "${test_INSTALL_DIR}/lib/cmake/NeLib/")

NeLibConfig.cmake.in文件内容为:

@PACKAGE_INIT@
set(NeLib_VERSION "1.0.0")
set(NeLib_SOVERSION "1.0")

find_library(NeLib_LIBRARIES
    NAMES libNeLib.a
    HINTS ${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(NeLib
    REQUIRED_VARS NeLib_LIBRARIES
    VERSION_VAR NeLib_VERSION
)

if(NeLib_FOUND)
    add_library(NeLib::NeLib INTERFACE IMPORTED)
    set_target_properties(NeLib::NeLib PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}"
        IMPORTED_LOCATION "${NeLib_LIBRARIES}"
        INTERFACE_LINK_LIBRARIES "OpenCV::opencv_core"
    )
endif()

然后我们安装这个库:

  • cd NeLib && mkdir 构建 && cd 构建
  • cmake ..-G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++
  • cmake --构建.
  • sudo cmake --install .

sudo cmake --install .
命令截获部分信息如下:

-- Install configuration: ""
-- Up-to-date: /usr/lib/libNeLib.a
-- Up-to-date: /usr/lib/Image.cppm
-- Up-to-date: /usr/lib/cmake/NeLib/NeLib.cmake
-- Up-to-date: /usr/lib/cmake/NeLib/NeLib-noconfig.cmake
-- Installing: /lib/cmake/NeLib/NeLibConfig.cmake

在新项目中使用该库: CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(untitled)
set(CMAKE_CXX_STANDARD 23)
find_package(NeLib REQUIRED)
add_executable(untitled
    main.cpp
)
target_link_libraries(untitled
    NeLib
)

主.cpp

import Image;

int main () {
    nl::Image image("../desk.png");
    image.show("show");
    return 0;
}

编译错误:

/opt/clion/bin/cmake/linux/x64/bin/cmake --build /home/yongheng/Projects/untitled/cmake-build-debug --target untitled -j 10
[3/4] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
FAILED: CMakeFiles/untitled.dir/main.cpp.o 
/usr/bin/clang++   -g -std=gnu++23 -fcolor-diagnostics -MD -MT CMakeFiles/untitled.dir/main.cpp.o -MF CMakeFiles/untitled.dir/main.cpp.o.d @CMakeFiles/untitled.dir/main.cpp.o.modmap -o CMakeFiles/untitled.dir/main.cpp.o -c /home/yongheng/Projects/untitled/main.cpp
/home/yongheng/Projects/untitled/main.cpp:1:8: fatal error: module 'Image' not found
    1 | import Image;
      | ~~~~~~~^~~~~
1 error generated.
ninja: build stopped: subcommand failed.
c++ cmake c++-modules
1个回答
0
投票

CMakeLists.txt 至:

cmake_minimum_required(VERSION 3.29)
project(NeLib)

# set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(VERSION "1.0.0")
set(CMAKE_CXX_STANDARD 23)
find_package(OpenCV REQUIRED)

add_library(NeLib STATIC )

file(GLOB module_files "*.cppm")

configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/NeLibConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/NeLibConfig.cmake"
    @ONLY
)
target_sources(NeLib
    PUBLIC
    FILE_SET cxx_modules TYPE CXX_MODULES FILES
    ${module_files}
)

target_compile_features(NeLib PUBLIC cxx_std_23)

install(TARGETS  ${PROJECT_NAME}  DESTINATION /usr/lib EXPORT ${PROJECT_NAME} FILE_SET cxx_modules)
install(EXPORT ${PROJECT_NAME} DESTINATION /usr/lib/cmake/NeLib/)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/NeLibConfig.cmake"
        DESTINATION "${test_INSTALL_DIR}/lib/cmake/NeLib/")

NeLibConfig.cmake.in 到:

@PACKAGE_INIT@
include ( "${CMAKE_CURRENT_LIST_DIR}/NeLib.cmake" )
© www.soinside.com 2019 - 2024. All rights reserved.