我正在使用 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.
尽管已成功复制
libcryptopp.so
,但首次运行时并未生成 CMakeLists.txt
文件,不幸的是,我需要使用 CLion 中的“重建项目”选项来生成 libcryptopp.so
并填充 ${CRYPTOPP_LIB}
变量。
有一种方法可以使 Cryptopp 的
CMakeLists.txt
文件在第一次运行时影响我的构建并生成 libcryptopp.so
文件吗?
如果项目使用
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()
命令指定。
(由 add_library
创建)时:
# 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}
)