gpt4 book ai didi

Cmake 安装目标触发

转载 作者:行者123 更新时间:2023-12-04 21:17:02 24 4
gpt4 key购买 nike

我正在使用 cmake 编写构建配置:除了带有自己代码的主项目之外,还有一些外部库。为了轻松更新这些库(zlib、libpng、...),我不想修改其 cmakelists 文件,但我需要特定的库目标(例如在 target_link_libraries() 中使用)。
另一个限制是我不能只说我的代码需要安装外部库,所有东西都必须位于一个源代码树中并且必须一起构建。
为了保持库提供的所有结构化(库、头文件),我想将库安装(如 make install 那样)到本地构建文件夹,然后包含生成的 cmake 文件以将所需的目标导入到我的项目中。

我想流程如下:

  • 使用 add_subdirectory() 构建外部库
  • 将外部库文件安装到本地目录
  • 使用生成的 cmake 文件导入目标
  • 在主项目 cmake 文件中使用导入的目标

  • 问题是自动执行第 2 步(需要在主项目 CMakeLists.txt 中触发 install 目标之后 add_subdiretory)。我可以构建和安装所有库,然后构建自己的代码,但这并不方便。

    所以问题是如何告诉 cmake 在构建期间进行中间安装?

    这里有一个小工作示例:

    文件结构:

    prj/CMakeLists.txt
    prj/src/main.cpp
    lib/CMakeLists.txt
    lib/include/libheader.h
    lib/src/libsource.cpp

    prj/CMakeLists.txt

    project(TestProject)
    cmake_minimum_required(VERSION 2.8)
    set(CMAKE_VERBOSE_MAKEFILE on)
    set(WORK_DIR ${CMAKE_CURRENT_SOURCE_DIR})
    # supposed to use add_subdirectory here (with forced install).
    # and then include prespecified include-file as here.
    include_directories(${WORK_DIR}/../lib/build/install/include)
    include(${WORK_DIR}/../lib/build/install/lib/libtargets.cmake)
    add_executable(main ${WORK_DIR}/src/main.cpp)
    target_link_libraries(main library_target)

    库/CMakeLists.txt

    project(TestLib)
    cmake_minimum_required(VERSION 2.8)
    set(CMAKE_VERBOSE_MAKEFILE on)
    set(WORK_DIR ${CMAKE_CURRENT_SOURCE_DIR})
    include_directories(${WORK_DIR}/include)
    add_library(library_target STATIC ${WORK_DIR}/src/libsource.cpp)
    set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install)
    install(FILES ${WORK_DIR}/include/libheader.h DESTINATION include)
    install(TARGETS library_target DESTINATION lib EXPORT libtargets)
    install(EXPORT libtargets DESTINATION lib)

    prj/src/main.cpp

    #include <iostream>
    #include "libheader.h"

    using std::cout;

    int main()
    {
    cout << getRandomNumber() << "\n";
    return 0;
    }

    lib/include/libheader.h

    #ifndef _LIBHEADER_H_
    #define _LIBHEADER_H_

    int getRandomNumber();

    #endif

    lib/src/libsource.cpp

    #include <iostream>
    #include "libheader.h"

    int getRandomNumber()
    {
    return 4; // guaranteed to be random.
    }

    您可以使用以下命令来构建所有:

    pushd . && mkdir lib/build && cd lib/build && cmake .. && make && make install && popd && pushd . && mkdir prj/build && cd prj/build && cmake .. && make || popd

    编辑:主项目中所需的 cmakelists:

    set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install)
    add_subdirectory(${WORK_DIR}/../lib ${CMAKE_CURRENT_BINARY_DIR}/lib.d)
    # force install here, somehow
    # because if not we will get "include could not find load file" here.
    include(${CMAKE_CURRENT_BINARY_DIR}/install/lib/libtargets.cmake)
    include_directories(${CMAKE_CURRENT_BINARY_DIR}/install/include)

    最佳答案

    抱歉,我无权发表评论

    所以,我把引用链接放在这里

    https://cmake.org/Bug/view.php?id=14311

    this is also useful when including prebuilt library binaries in the project.

    add_library(prebuilt STATIC IMPORTED) set_property(TARGET prebuilt PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libprebuilt.a)

    install(TARGETS other_built_targets prebuilt EXPORT project-export RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib )

    Making prebuilt an imported target makes in convenient to use in the project and treats it further like any other target.

    关于Cmake 安装目标触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528749/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com