使用vcpkg 编译支持sftp的libcurl

2 篇文章 0 订阅

libcurl的编译

下载vcpkg

使用git将vcpkg从github上clone下来:github地址

git clone https://github.com/microsoft/vcpkg

编译vcpkg

使用cmd进入vcpkg所在目录执行批处理文件完成安装。

F:\vcpkg>cd vcpkg
F:\vcpkg\vcpkg>bootstrap-vcpkg.bat

使用vcpkg编译所需的库

进入vckpkg目录,使用vckpkg install 命令进行安装。在安装前可使用vcpkg search命令进行库的检索。
以下是所有的curl库,如果要支持sftp则需要安装支持ssh的curl。

F:\vcpkg\vcpkg>vcpkg search curl
cpr                  1.3.0-8          C++ Requests is a simple wrapper around libcurl inspired by the excellent Pyth...
curl                 7.71.1#2         A library for transferring data with URLs
curl[brotli]                          brotli support (brotli)
curl[c-ares]                          c-ares support
curl[http2]                           HTTP2 support
curl[mbedtls]                         SSL support (mbedTLS)
curl[non-http]                        Enables protocols beyond HTTP/HTTPS/HTTP2
curl[openssl]                         SSL support (OpenSSL)
curl[sectransp]                       SSL support (sectransp)
curl[ssh]                             SSH support via libssh2
curl[ssl]                             Default SSL backend
curl[sspi]                            SSPI support
curl[tool]                            Builds curl executable
curl[winssl]                          SSL support (Secure Channel / "WinSSL")
curlpp               2018-06-15-3     C++ wrapper around libcURL
czmq[curl]                            Build with libcurl
fmi4cpp[curl]                         Allows loading FMUs from URL
oatpp-curl           1.0.0            Oat++ Modern web framework curl module to use libcurl as a RequestExecutor on ...
restclient-cpp       0.5.2            Simple REST client for C++. It wraps libcurl for HTTP requests.

使用安装命令进行编译所需要的库,可以指定所要编译库的信息如:动态链接/静态链接,x86/x64等信息。以下命令编译的为x86适用于windows的静态链接库。

F:\vcpkg\vcpkg>vcpkg install curl[ssh]:x86-windows-static

不指定静态还是动态编译的话会自动将静/动态都编译出来。
接下来就可以等待编译成功了。vcpkg需要下载所要编译包的各种依赖文件,而其下载会特别慢推荐这篇文章解决下载慢的解决文章

vs中使用vcpkg

推荐这篇文章4.0之后部分vs中使用vcpkg

libcurl文件上传使用实例

头文件中引入#inclde<curl/curl>

其中MYRESULT为一个枚举类型
using MYRESULT = enum 
{
	SECCESS = 0,
	OPEN_FILE_ERROR,
	UPLOAD_FILE_ERROR
};

int32_t FileUpLoad(const std::string& user, const std::string& passwd, const std::string& sftpPath, 
	const std::string& filePath, int32_t& curlErrorCode)
{
	int32_t myres = 0;

	CURL* curl = nullptr;
	CURLcode res;
	std::string userPwd = user + ":" + passwd;
	std::cout << userPwd << std::endl;
	
	//打开所需上传的文件
	FILE* pSendFile = nullptr;
	struct stat file_info;
	curl_off_t iFileSize;

	if (stat(filePath.c_str(), &file_info))
	{
		return MYRESULT::OPEN_FILE_ERROR;
	}

	iFileSize = (curl_off_t)file_info.st_size;   //获取文件大小

	fopen_s(&pSendFile, filePath.c_str(), "rb"); //打开文件

	if (nullptr == pSendFile)
	{
		return MYRESULT::OPEN_FILE_ERROR;
	}

	curl_global_init(CURL_GLOBAL_ALL);

	curl = curl_easy_init();

	if (curl) {
		curl_easy_setopt(curl, CURLOPT_URL, sftpPath.c_str());		//设置sftp的路径
		curl_easy_setopt(curl, CURLOPT_USERPWD, userPwd.c_str());	//设置用户名和密码	
		curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);//设置回调函数
		curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);		//设置要上传文件的指针
		curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); //选择是否在远程文件夹不存在时创建,1为创建
		curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);                 //上传
		curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, iFileSize);//设置文件大小
		curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 120);
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		//获取curl的执行结果
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		if (CURLE_OK != res)
		{
			curlErrorCode = res;
			myres = MYRESULT::UPLOAD_FILE_ERROR;
		}
	}
	else
	{
		myres = MYRESULT::UPLOAD_FILE_ERROR;
	}

	//关闭文件及curl清理
	fclose(pSendFile);
	curl_global_cleanup();
	return myres;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值