使用 cryptopp-cmake 和 crypto++ 一起创建一个 CMakeLists.txt 项目文件
Using cryptopp-cmake and crypto++ toghter a CMakeLists.txt project file
我正在使用 Clion,我正在尝试在我的 CMakeLists.txt 文件中使用 cryptopp-cmake for 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 文件=39=] 不幸的是,我需要使用 CLion 中的“重建项目”选项来生成和填充 libcryptopp.so ${CRYPTOPP_LIB}
变量。
是一种使 Cryptopp 的 CMakeLists.txt 文件在第一个 运行 和生成 libcryptopp.so 文件?
如果项目使用 add_library
创建库,则 link 使用 target 名称创建该库。在这种情况下不要使用库 file。
根据 cryptopp 项目的 CMakeLists.txt,库目标的名称是 cryptopp-shared
,所以只需 link 即可:
target_link_libraries(hmmenc-client PRIVATE cryptopp-shared)
详细解释
您 link 您的可执行文件带有生成的库文件。所以你需要确定,这个文件的生成是在 在 你的可执行文件 linking 之前执行的。
在 CMake 中,动作之间的顺序由 目标 之间的 依赖关系 指定。因此,您的可执行文件应从库中指定为 dependent 作为 target。在 CMake 中,目标之间的依赖关系由 add_dependencies()
命令指定。
但是,CMake 为 link 提供了一种方便的方法,同时使用库并指定依赖项。当您 link 使用库 target(由 add_library
创建)时:
- CMake 知道哪个库文件对应于该库目标,因此它使用该文件进行实际 linking。
- 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(hmmenc-client PRIVATE
${CRYPTOPP}
)
我正在使用 Clion,我正在尝试在我的 CMakeLists.txt 文件中使用 cryptopp-cmake for 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 文件=39=] 不幸的是,我需要使用 CLion 中的“重建项目”选项来生成和填充 libcryptopp.so ${CRYPTOPP_LIB}
变量。
是一种使 Cryptopp 的 CMakeLists.txt 文件在第一个 运行 和生成 libcryptopp.so 文件?
如果项目使用 add_library
创建库,则 link 使用 target 名称创建该库。在这种情况下不要使用库 file。
根据 cryptopp 项目的 CMakeLists.txt,库目标的名称是 cryptopp-shared
,所以只需 link 即可:
target_link_libraries(hmmenc-client PRIVATE cryptopp-shared)
详细解释
您 link 您的可执行文件带有生成的库文件。所以你需要确定,这个文件的生成是在 在 你的可执行文件 linking 之前执行的。
在 CMake 中,动作之间的顺序由 目标 之间的 依赖关系 指定。因此,您的可执行文件应从库中指定为 dependent 作为 target。在 CMake 中,目标之间的依赖关系由 add_dependencies()
命令指定。
但是,CMake 为 link 提供了一种方便的方法,同时使用库并指定依赖项。当您 link 使用库 target(由 add_library
创建)时:
- CMake 知道哪个库文件对应于该库目标,因此它使用该文件进行实际 linking。
- 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(hmmenc-client PRIVATE
${CRYPTOPP}
)