FindCURL: support static linking
The best method
for use libcurl in CMake project is to use find_package(... CONFIG)
mode. But this is only available:
- with cURL version >=7.57
- when cURL binaries are build with CMake
Most of the cURL binary packages are distributed without *.cmake config files because they are build with autotools.
Current CMake FindCURL module
tries to guess cURL configuration using only filenames analysis. This is not enough - some important compiler and linker options are ignored resulting in non-working CMake project configuration.
Compiler part
libcurl uses the same public headers for static and dynamic library, with some conditional #define
on Windows platform. CURL_STATICLIB
must be defined to compile project for future static linking
Linker part
libcurl is specific monolithic library that can be build in many different ways. There are multiple params to include/exclude specific protocols etc. resulting different dependencies for each libcurl build. It's almost impossible to guess all dependencies without knowledge of libcurl build-time configuration. Current FindCURL approach cannot handle this.
Proposal
- Rewrite FindCURL module to use pkg-config -
pkgconfig/libcurl.pc
seems to be included in majority of binary packages. This will allow to read proper linker parameters - Analyse libcurl library filename (is there any better way for this?) to detect static/dynamic library type then add CURL_STATICLIB definition if required
Example libcurl.pc file from Windows:
prefix=C:/curl-7_56_0/vc141_x64_Release_dll
exec_prefix=${prefix}
libdir=C:/curl-7_56_0/vc141_x64_Release_dll/lib
includedir=${prefix}/include
supported_protocols="FTP FTPS HTTP HTTPS"
supported_features="WinSSL IPv6 AsynchDNS SSPI SPNEGO Kerberos NTLM"
Name: libcurl
URL: https://curl.haxx.se/
Description: Library to transfer files with ftp, http, etc.
Version: 7.56.0-DEV
Libs: -L${libdir} -lcurl
Libs.private: -lwinmm -lws2_32 -lcrypt32
Cflags: -I${includedir}
Libs.private
depends on supported_protocols
and supported_features
and must be implemented in FindCURL to allow static linking in CMake projects