Search code examples
curlcmake

Target not declared when using FetchContent on curl


I have a CMake project which is supposed to use curl to connect to interact with an HTTP server. No HTTPS, no bells, no whistles, strictly minimal.

This is the CMake-File I came up with thus far.

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
include(FetchContent)

project(MyProject)

add_executable(${PROJECT_NAME} main.c)

set(CURL_MINIMAL 1 CACHE INTERNAL "Minimal curl build")

# **Build static library**
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build libcurl as static library")
set(BUILD_STATIC_LIBS ON CACHE INTERNAL "Build static lib")
#set(CURL_STATICLIB ON CACHE BOOL "Force static linking")

# **Minimal Feature-Set**
set(CURL_USE_OPENSSL OFF CACHE INTERNAL "Disable OpenSSL")
set(CURL_USE_LIBSSH2 OFF CACHE INTERNAL "Disable SSH2")
set(CURL_USE_LIBIDN2 OFF CACHE INTERNAL "Disable IDN2")
set(CURL_USE_GSSAPI OFF CACHE INTERNAL "Disable GSSAPI")
set(CURL_USE_LIBPSL OFF CACHE INTERNAL "Disable libpsl support")
set(CMAKE_USE_LIBIDN2 OFF CACHE INTERNAL "Disable IDN2")
set(CMAKE_USE_ARES OFF CACHE INTERNAL "Disable c-ares")
set(HTTP_ONLY ON CACHE INTERNAL "Only enable HTTP support")
set(CURL_DISABLE_LDAP ON CACHE INTERNAL "Disable LDAP")
set(CURL_DISABLE_TFTP ON CACHE INTERNAL "Disable TFTP")
set(CURL_DISABLE_FTP ON CACHE INTERNAL "Disable FTP")
set(CURL_DISABLE_FILE ON CACHE INTERNAL "Disable FILE protocol")
set(CURL_DISABLE_DICT ON CACHE INTERNAL "Disable DICT protocol")
set(CURL_DISABLE_GOPHER ON CACHE INTERNAL "Disable GOPHER protocol")
set(CURL_DISABLE_IMAP ON CACHE INTERNAL "Disable IMAP protocol")
set(CURL_DISABLE_POP3 ON CACHE INTERNAL "Disable POP3 protocol")
set(CURL_DISABLE_RTSP ON CACHE INTERNAL "Disable RTSP protocol")
set(CURL_DISABLE_SMB ON CACHE INTERNAL "Disable SMB protocol")
set(CURL_DISABLE_SMTP ON CACHE INTERNAL "Disable SMTP protocol")
set(CURL_DISABLE_TELNET ON CACHE INTERNAL "Disable TELNET protocol")
set(CURL_DISABLE_LDAP ON CACHE INTERNAL "Disable LDAP protocol")
set(CURL_DISABLE_HSTS ON CACHE INTERNAL "Disable HSTS support")

# **Skip the rest too**
set(BUILD_CURL_EXE OFF CACHE INTERNAL "Do not build curl CLI tool")
set(BUILD_TESTING OFF CACHE INTERNAL "Disable tests")
set(BUILD_EXAMPLES OFF CACHE INTERNAL "Disable examples")
set(BUILD_LIBCURL_DOCS OFF CACHE INTERNAL "Disable docs")
set(BUILD_MISC_DOCS OFF CACHE INTERNAL "Disable docs")

FetchContent_Declare(curl
    GIT_REPOSITORY https://github.com/curl/curl.git
    GIT_TAG curl-8_12_0
)

FetchContent_MakeAvailable(curl)

target_link_libraries(${PROJECT_NAME} PRIVATE curl)

This results in the linker error cannot find -lcurl.
When I checked the output, I realized, that the curl library was not finished building when my application already started linking, which, in turn, suggests, that something in the dependency tree is wrong.

When I tried putting add_dependencies(${PROJECT_NAME} curl) into my CMake file, the error message was The dependency target "curl" of target "MyProject" does not exist.

When building the two projects independently from each other, it seems to work and the static library I want is output in the build tree.

But this is, of course, not the desired behavior. Any idea, where I messed up?


Solution

  • Checking the actually generated project tree helped.

    I did not have to link to curl, but to libcurl_static (because I wanted to build the static library).

    Leaving this here for anyone else with the same problem.