使用cryptopp-cmake和crypto++一起生成CMakeLists.txt项目文件

xriantvc  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(309)

我正在使用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(my_project PRIVATE
        ${CRYPTOPP_LIB}
)

当我让它在CLion中运行时,CMakeLists.txt得到了应有的复制,但我得到了错误

No rule to make target '_deps/cryptopp-build/libcryptopp.so', needed by '../my_project/bin/my_project'.  Stop.

尽管成功复制了CMakeLists.txt,但在第一次运行时没有生成libcryptopp.so文件,不幸的是,我需要在 CLion 中使用“rebuild project”选项来生成libcryptopp.so并填充${CRYPTOPP_LIB}变量。
是否有一种方法可以使Cryptopp的CMakeLists.txt文件在第一次运行时影响我的构建并生成libcryptopp.so文件?

dvtswwa3

dvtswwa31#

如果一个项目创建了一个add_library的库,那么使用target名称链接到该库。在这种情况下不要使用库文件
根据cryptopp项目的CMakeLists.txt,库目标的名称是cryptopp-shared,所以只需链接它:

target_link_libraries(hmmenc-client PRIVATE cryptopp-shared)

详细说明

您将可执行文件与生成的库文件链接。所以你需要确保,这个文件的生成是在链接你的可执行文件之前执行的。
在CMake中,操作之间的顺序由目标之间的依赖关系指定。因此,您的可执行文件应该被指定为dependent从库作为目标。在CMake中,目标之间的依赖关系由add_dependencies()命令指定。
然而,CMake提供了一种方便的方法,可以同时链接到库并指定依赖项。当您链接到库target(由add_library创建)时:

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

p8h8hvxi2#

**更新:**如果使用CRYPTOPP_8_6_0以上版本的cryptopp-cmake,该流程需要更新,将无法正常工作。

我最终在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(my_project PRIVATE
        ${CRYPTOPP}
)

相关问题