I'm using Clion and I'm trying to use cryptopp-cmake for cryptopp in my CMakeLists.txt
file, since Cryptopp doesn't have a CMakeLists.txt
file in its default project repository. My CMakeLists.txt
has this Cryptopp related content:
# 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}
)
When I let it run in CLion the CMakeLists.txt
gets copied as it should but I get the error
No rule to make target '_deps/cryptopp-build/libcryptopp.so', needed by '../my_project/bin/my_project'. Stop.
The libcryptopp.so
file is not being generated on the first run despite of the successfully copied CMakeLists.txt
and unfortunately, I need to use the "rebuild project" option in CLion for libcryptopp.so
to be generated and populate the ${CRYPTOPP_LIB}
variable.
Is the a way to make Cryptopp's CMakeLists.txt
file effect my build on the first run and generate the libcryptopp.so
file?
If a project creates a library with add_library
, then link with that library using the target name. Do not use the library file in that case.
According to the cryptopp project's CMakeLists.txt, the name of the library target is cryptopp-shared
, so just link with it:
target_link_libraries(hmmenc-client PRIVATE cryptopp-shared)
You link your executable with a library file which is generated. So you need to be sure, that generation of this file is performed before the linking of your executable.
In CMake order between actions is specified by dependency between the targets. So your executable should be specified as dependent from the library as a target. In CMake a dependency between targets is specified by add_dependencies()
command.
However, CMake provides a convenient way for link with a library and specifying the dependency at the same time. When you link with a library target (created by add_library
):