How to Make cURL works with cmake

8 篇文章 0 订阅
How to Make cURL works with cmake

    Recently, I want to learn cURL, so downloaded it and compiled it, and use
cmake to manage the project, I wrote a CMakelists.txt as below:

cmake_minimum_required(VERSION 2.8)
project(curlTest)
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})
set(sources appMain.cpp)
add_executable(curlTest ${sources})
target_link_libraries(curlTest ${CURL_LIBRIRIES})



    But it did not work, and cmake reported that it can not find cURL, I met
this situation before, so I set the environment variable for cmake like this:
 
    set LIBCURL_ROOT= "cURL's path"

    But it did not work neither, and I was confused, what was wrong with me, and
how could I make it works?
    So I configured the environment variable CPLUS_INCLUDE_PATH to libcurl's
path, it did not work neither.
    Finally, I open the file FindCURL.cmake and check content, I found that the
content didn't point the path of libcurl out, so I modified sentence in
FindCURL.cmake:

find_path(CURL_INCLUDE_DIR
    NAMES curl/curl.h)


to

find_path(CURL_INCLUDE_DIR
    NAMES curl/curl.h
    PATHS $ENV{LIBCURL_ROOT}/include)


and the sentece in FindCURL.cmake

find_library(CURL_LIBRARY NAMES
    curl
  # Windows MSVC prebuilts:
    curllib
    libcurl_imp
    curllib_static
  # Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
    libcurl)
 


to


and run:
   cmkae .. -G "MinGW Makefiles", it worked.
    
    The conclution:
    1. don't trust the libraries so much.
    2. think more.
CURLcmake中有两种方式来查找和使用,分别是MODULE模式和CONFIG模式。MODULE模式是通过调用cmake内置的FindCURL.cmake脚本来查找CURL,而CONFIG模式是通过引用CURL生成的cmake脚本来查找CURL。 在CURLcmake脚本中,通过生成一个import target命名为CURL::libcurl来表示CURL的静态库。这个import target有一个属性INTERFACE_LINK_LIBRARIES,它定义了CURL::libcurl所依赖的其他库,包括"wldap32;winmm;ws2_32;OpenSSL::SSL;OpenSSL::Crypto;ZLIB::ZLIB;advapi32;crypt32"等库。 使用CONFIG模式来查找CURL时,先尝试使用CURLcmake脚本来查找,如果找不到则再使用MODULE模式来查找。这样可以保证在项目中能正确连接到CURL::libcurl target,并且找到它所依赖的其他库。这对于生成动态库或EXE等需要连接的动作非常重要,否则可能会导致连接失败。 因此,在使用CURLcmake时,建议优先使用CONFIG模式来查找CURL,以确保能够正常连接到CURL::libcurl target。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [cmake:Windows编译支持HTTPS的curl库及find_package查找CURL时需要注意的问题](https://blog.csdn.net/10km/article/details/122812211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值