【问题标题】:Using cryptopp-cmake and crypto++ toghter a CMakeLists.txt project file使用 cryptopp-cmake 和 crypto++ 一起创建 CMakeLists.txt 项目文件
【发布时间】:2020-12-21 16:34:53
【问题描述】:

我正在使用 Clion 并且我正尝试在我的 CMakeLists.txt 文件中使用 cryptopp-cmake 作为 cryptopp,因为 Cryptopp 在其默认项目存储库中没有 CMakeLists.txt 文件。我的 CMakeLists.txt 有这个 Cryptopp 相关内容:

# Include usage of external projects
include(FetchContent)

# Get the cryptopp CMakeLists.txt file for cryptopp package
set(CRYPTOPP_CMAKE "cryptopp-cmake")
FetchContent_Declare(
        ${CRYPTOPP_CMAKE}
        GIT_REPOSITORY  https://github.com/noloader/cryptopp-cmake
        GIT_TAG         CRYPTOPP_8_2_0
)

FetchContent_GetProperties(${CRYPTOPP_CMAKE})
if(NOT ${CRYPTOPP_CMAKE}_POPULATED)
    FetchContent_Populate(${CRYPTOPP_CMAKE})
endif()


# Get the cryptopp package
set(CRYPTOPP "cryptopp")
FetchContent_Declare(
        ${CRYPTOPP}
        GIT_REPOSITORY  https://github.com/weidai11/cryptopp
        GIT_TAG         CRYPTOPP_8_2_0
)

FetchContent_GetProperties(${CRYPTOPP})
if(NOT ${CRYPTOPP}_POPULATED)
    FetchContent_Populate(${CRYPTOPP})
endif()

# !!! IMORTANT !!! before using add_subdirectory(), include_directories() and set(CRYPTOPP_LIB....) commands.
file(COPY ${${CRYPTOPP_CMAKE}_SOURCE_DIR}/CMakeLists.txt DESTINATION ${${CRYPTOPP}_SOURCE_DIR})
add_subdirectory(${${CRYPTOPP}_SOURCE_DIR} ${${CRYPTOPP}_BINARY_DIR})
include_directories(${${CRYPTOPP}_SOURCE_DIR})
set(CRYPTOPP_LIB ${${CRYPTOPP}_BINARY_DIR}/libcryptopp.so)

# Link the project libraries to the executable
target_link_libraries(hmmenc-client PRIVATE
        ${CRYPTOPP_LIB}
)

当我让它在 CLion 中运行时,CMakeLists.txt 会按原样复制,但我收到错误 *** No rule to make target '_deps/cryptopp-build/libcryptopp.so', needed by '../hmmenc_client/bin/hmmenc-client'. Stop.

尽管成功复制了 CMakeLists.txt,但在第一次运行时没有生成 libcryptopp.so 文件,不幸的是,我需要使用“重建项目” CLion 中的选项,用于生成 libcryptopp.so 并填充 ${CRYPTOPP_LIB} 变量。

是否有一种方法可以使 Cryptopp's CMakeLists.txt 文件在第一次运行时影响我的构建并生成 libcryptopp.so 文件?

【问题讨论】:

  • 您可以考虑在 GitHub 上填写问题,以便作者(活跃的社区 contributor)可以提供见解和/或修复。

标签: c++ cmake crypto++


【解决方案1】:

如果项目使用add_library 创建库,则使用target 名称链接到该库。在这种情况下不要使用库 file

根据cryptopp项目的CMakeLists.txt,库目标的名称是cryptopp-shared,所以直接链接即可:

target_link_libraries(hmmenc-client PRIVATE cryptopp-shared)

详细说明

您将可执行文件与生成的库文件链接起来。因此,您需要确定,此文件的生成是在链接可执行文件之前执行的。

在 CMake 中,动作之间的顺序由 targets 之间的dependency 指定。所以你的可执行文件应该从库中指定为dependent作为target。在 CMake 中,目标之间的依赖关系由 add_dependencies() 命令指定。

但是,CMake 提供了一种方便的方式来链接库并同时指定依赖项。当您与库目标(由add_library 创建)链接时:

  1. CMake 知道哪个库文件对应于该库目标,因此它使用该文件进行实际链接。
  2. CMake 自动调整可执行目标和库目标之间的依赖关系。

【讨论】:

  • 我已经多次浏览这个 CMakeLists.txt 文件,但不知怎的,我设法错过了 cryptopp-shared 的部分。
【解决方案2】:

我最终在 Linux 下使用了这个设置:

# Include usage of external projects
include(FetchContent)

############################################################################################
# Get the CryptoPP CMakeLists.txt File for CryptoPP Package GIT_TAG Must be the Same
############################################################################################
message(CHECK_START "Fetching CryptoPP-CMAKE")
set(CRYPTOPP_CMAKE "cryptopp-cmake")
set(CRYPTOPP_GIT_TAG "CRYPTOPP_8_2_0")
FetchContent_Declare(
        ${CRYPTOPP_CMAKE}
        GIT_REPOSITORY  https://github.com/noloader/cryptopp-cmake
        GIT_TAG         ${CRYPTOPP_GIT_TAG}
)

FetchContent_GetProperties(${CRYPTOPP_CMAKE})
if(NOT ${CRYPTOPP_CMAKE}_POPULATED)
    FetchContent_Populate(${CRYPTOPP_CMAKE})
endif()


############################################################################################
# Get the CryptoPP Package
############################################################################################
message(CHECK_START "Fetching CryptoPP")
set(CRYPTOPP "cryptopp")
FetchContent_Declare(
        ${CRYPTOPP}
        GIT_REPOSITORY  https://github.com/weidai11/cryptopp
        GIT_TAG         ${CRYPTOPP_GIT_TAG}
)

FetchContent_GetProperties(${CRYPTOPP})
if(NOT ${CRYPTOPP}_POPULATED)
    FetchContent_Populate(${CRYPTOPP})
    # !!! IMPORTANT !!!
    # Copy the CMakeLists.txt file from https://github.com/noloader/cryptopp-cmake with same TAG CRYPTOPP repository into ${${CRYPTOPP}_SOURCE_DIR}
    # before using add_subdirectory(), include_directories() and set(CRYPTOPP_LIB....) commands, until the a proper COPY command was implemented.
    file(COPY ${${CRYPTOPP_CMAKE}_SOURCE_DIR}/CMakeLists.txt DESTINATION ${${CRYPTOPP}_SOURCE_DIR})
    add_subdirectory(${${CRYPTOPP}_SOURCE_DIR} ${${CRYPTOPP}_BINARY_DIR})
endif()
include_directories(${${CRYPTOPP}_SOURCE_DIR})

# Set Cryptopp library properties every time after the first population
if(${CRYPTOPP}_POPULATED)
    # Build shared or static library
    set(BUILD_SHARED_CRYPTOPP_OLD ${BUILD_SHARED})
    set(BUILD_SHARED ON CACHE INTERNAL "Build CryptoPP SHARED libraries")
    message("Build CryptoPP shared lib is set to: ${BUILD_SHARED}")
    if(${BUILD_SHARED} STREQUAL "ON")
        set(CRYPTOPP "cryptopp-shared")
        else ()
            set(CRYPTOPP "cryptopp-static")
    endif()
    set(BUILD_SHARED ${BUILD_SHARED_CRYPTOPP_OLD} CACHE BOOL "Type of libraries to build" FORCE)
endif()

# Link the project libraries to the executable
target_link_libraries(hmmenc-client PRIVATE
        ${CRYPTOPP}
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多