C++之libcurl环境配置安装

C++之libcurl环境配置安装


前言

最近在折腾c++的网络通信, 又想具备跨平台这一特性的, 于是就选了curl这个网络库来使用


curl与libcurl的区别

curl基于libcurl做出来的一个工具, 可以运行在各个平台

libcurl是一个库来的, curl的底层就是对这个库进行封装开发, 如果想在自己的项目中使用网络通信, 还得导入这个库到项目中


Install libcurl on Linux

ubuntu-20.04举例, libcurl有三个包可以选择, 分别为以下三个

libcurl4-gnutls-dev
libcurl4-nss-dev
libcurl4-openssl-dev
  • 这三个包有什么区别呢?
  • 进行https访问时的ssl协议库用的不一样, 分别用的gnutls, nss, openssl

这里选择openssl的安装, 等待安装完成即可使用了~

sudo apt install libcurl4-openssl-dev libcurl4-doc libidn11-dev libkrb5-dev libldap2-dev librtmp-dev libssh2-1-dev libssl-dev zlib1g-dev

如果不选择上述方法安装, 还可以下载源码到本地编译安装

wget https://curl.se/download/curl-7.85.0.tar.gz
tar -vxzf curl-7.85.0.tar.gz
cd curl-7.85.0
./configure --with-openssl
make
make install

具体可以参考 https://github.com/curl/curl/blob/master/docs/INSTALL.md#unix


Install libcurl on Windows

windows平台无论用的是visual studio还是visual studio code 包管理我都推荐用vcpkg

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install libcurl:x64-windows

不指定平台的话默认是x32的, 如果想设置默认平台, 可以添加环境变量export VCPKG_DEFAULT_TRIPLET=x64-windows


源码例子

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
/* <DESC>
 * Simple HTTPS GET
 * </DESC>
 */
#include <stdio.h>
#include <curl/curl.h>
 
int main(void)
{
    
    
  CURL *curl;
  CURLcode res;
 
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
  curl = curl_easy_init();
  if(curl) {
    
    
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/");
 
#ifdef SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who is not using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
 
#ifdef SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you are connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
 
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
 
  curl_global_cleanup();
 
  return 0;
}

更多例子可以参考:


其他的网络库

还有很多与curl功能一样的库, 比如: (排名不分先后)

猜你喜欢

转载自blog.csdn.net/a924282761/article/details/127174871