我在CMakeLists.txt
中使用FetchContent
从源代码构建curl
和curlpp
,然后在CMakeLists.txt
中使用可执行文件。
cmake_minimum_required(VERSION 3.17)
project(myproject VERSION 1.0.0)
include(FetchContent)
include(CMakePrintHelpers)
cmake_print_variables(CMAKE_CXX_FLAGS)
if (WIN32)
set(CMAKE_USE_SCHANNEL ON)
endif()
# --- CURL
FetchContent_Declare(curl
URL https://github.com/curl/curl/releases/download/curl-7_75_0/curl-7.75.0.tar.xz
URL_HASH SHA256=fe0c49d8468249000bda75bcfdf9e30ff7e9a86d35f1a21f428d79c389d55675 # the file hash for curl-7.75.0.tar.xz
USES_TERMINAL_DOWNLOAD TRUE)
FetchContent_MakeAvailable(curl)
add_library(curl_int INTERFACE)
target_link_libraries(curl_int INTERFACE libcurl)
target_include_directories(curl_int INTERFACE ${curl_SOURCE_DIR}/include ${curl_BINARY_DIR}/include/curl)
add_library(CURL::mylibcurl ALIAS curl_int)
set(CURL_INCLUDE_DIR
${curl_SOURCE_DIR}/include
)
# --- CURL C++
FetchContent_Declare(curlpp
GIT_REPOSITORY https://github.com/jpbarrette/curlpp.git
)
FetchContent_MakeAvailable(curlpp) # ----> Error occurs here
cmake_print_variables(curlpp_SOURCE_DIR curlpp_BINARY_DIR)
add_library(curlpp_int INTERFACE)
target_link_libraries(curlpp_int INTERFACE libcurlpp CURL::mylibcurl)
target_include_directories(curlpp_int INTERFACE ${curlpp_SOURCE_DIR}/include ${curlpp_BINARY_DIR}/include/curl)
add_library(CURLPP::mylibcurlpp ALIAS curlpp_int)
set(CURLPP_INCLUDE_DIR
${curlpp_SOURCE_DIR}/include
)
set(CURLPP_LIB_DIR
${curlpp_BINARY_DIR})
cmake_print_variables(CURLPP_LIB_DIR)
cmake_print_variables(CURLPP::mylibcurlpp)
# The rest of CMakeLists.txt is omitted
然而,在cmake构建期间,当它执行FetchContent_MakeAvailable(curlpp)
时,我得到一个关于
Could NOT find CURL (missing: CURL_LIBRARY)
我通过target_link_libraries(curlpp_int INTERFACE libcurlpp CURL::mylibcurl)
链接curlpp
和curl
,并期望它能够找到它。
我该如何解决这个问题?
在CURL的CMake中,CURL库是必需的,如果不可用则获取:
71 message(STATUS "Looking for CURL")
72 find_package(CURL REQUIRED)
73
. if(CURL_FOUND)
. message(STATUS "Found CURL version: ${CURL_VERSION_STRING}")
. message(STATUS "Using CURL include dir(s): ${CURL_INCLUDE_DIRS}")
. message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}")
. else()
. message(STATUS "Fetching CURL")
. include(FetchContent)
. FetchContent_Declare(CURL GIT_REPOSITORY https://github.com/curl/curl.git)
. FetchContent_MakeAvailable(CURL)
. endif()
- https://github.com/jpbarrette/curlpp/blob/master/CMakeLists.txt当
我可以想到这里的两个问题,但更多的信息可能会有所帮助,例如,如果您的错误抛出在第72行或以上。
- 一个选项是,将CURL构建到一个在curlpp find_package调用中找不到的目录中。
- 另一个选择是你的版本不兼容,看看curl的代码,他们默认会下载curl的主repo。