I am building curl
and curlpp
from source in my CMakeLists.txt
using FetchContent
to use in an executable later in my 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
However, during cmake build, when it executes FetchContent_MakeAvailable(curlpp)
, I get a complaint about
Could NOT find CURL (missing: CURL_LIBRARY)
I am linking curlpp
with curl
by target_link_libraries(curlpp_int INTERFACE libcurlpp CURL::mylibcurl)
and was expecting it to be able to find it.
How could I solve this?
CodePudding user response:
You can try adding this line after setting the CURL_INCLUDE_DIR:
find_library(CURL_LIBRARY NAMES curl PATHS ${curl_BINARY_DIR}) target_link_libraries(curl_int INTERFACE ${CURL_LIBRARY})
This will search for the curl library in the specified directory and link it to your curl_int target.
CodePudding user response:
In the CMake of curlpp, the CURL library is required and fetched if not available:
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()
I could think of two problems here, but also more information might be helpful, for example, if your error is thrown in line 72 or above that.
- One option would be, that you build CURL into a directory that then is not found in the curlpp find_package call.
- Another option would be that your versions are not compatible, looking at the code from curlpp they by default would download the master repo of curl.