在windows下,编译可访问https的libcurl静态库过程

1. 首先编译openssl静态库

整个过程用的是 vs2015命令提示工具

需要安装perl
下载地址:添加链接描述
解压后, 在 openssl-1.0.2e目录下创建develop这个文件夹, cd到 openssl-1.0.2e目录下,

a.配置编译文件和模式
执行

perl Configure VC-WIN32 no-asm --prefix=E:\curl\openssl-1.0.2e\develop

VC-WIN32标识windows 32位操作系统,
64位用VC-WIN64A表示, 若要使用debug版本,请使用debug-VC-WIN64A或debug-VC-WIN32
no-asm 表示不用汇编
–prefix=E:\curl\openssl-1.0.2e\develop 是设置安装目录
b.生成编译配置文件
若为Windows 64位系统,执行

ms\do_win64a.bat

若为Windows 32位系统,执行

ms\do_ms.bat

执行这一步之后,在ms目录下会生成nt.mak和ntdll.mak两个编译配置文件
nt.mak 用于生成静态lib库
ntdll.mak 用于生成动态dll库
c.编译
静态库

nmake -f ms\nt.mak	

动态库

nmake -f ms\ntdll.mak

d.测试
测试静态库:

 nmake -f ms\nt.mak test

测试动态库:

 nmake -f ms\ntdll.mak test

若最终显示 passed all tests 则说明生成的库正确
e.安装
安装静态库:

nmake -f ms\nt.mak install

安装动态库:

nmake -f ms\ntdll.mak install

编译安装完成后,会在E:\curl\openssl-1.0.2e\develop目录下看到生成的相关文件夹
f.清楚上次编译
清除上次静态库的编译,以便重新编译:

nmake -f ms\nt.mak clean

清除上次动态库的编译,以便重新编译:

nmake -f ms\ntdll.mak clean

参考:https://blog.csdn.net/mayue_web/article/details/83997969,非常详细

2.编译zlib

a.下载源码
http://zlib.net/zlib-1.2.11.tar.gz
解压后,在zlib根目录下 命令行执行
nmake -f win32/Makefile.msc

3.编译libcurl

a.下载源码
http://curl.haxx.se/download/curl-7.46.0.tar.bz2
b.拷贝所需的openssl与zlib相关库与头文件
.根据curl源代码根目录下winbuild目录下BUILD.WINDOWS.txt的提示
在源代码同级的目录下建立deps文件夹
此时的目录结构如下图(伪目录

somedirectory\
|__curl-src
|    |_winbuild
|
|__deps
  |_ lib
  |_ include
  |_ bin

我这里的deps文件夹目录是 E:\curl\deps
将编译openssl时候,安装目录E:\curl\openssl-1.0.2e\develop\include下的
openssl夹拷贝到E:\curl\deps\include\目录下;
将zlib源码根目录下的zconf.h、zlib.h和zutil.h拷贝到E:\curl\deps\include目录下。
将E:\curl\openssl-1.0.2e\develop\lib目录下的libeay32.lib和ssleay.lib拷贝到E:\curl\deps\lib目录下;
将zlib源码根目录下的zlib.lib拷贝到E:\curl\deps\lib目录下。
c.编译libcurl
命令行进入源代码根目录下 winbuild 目录

nmake RTLIBCFG=static /f Makefile.vc mode=static VC=14 WITH_DEVEL=E:\curl\deps WITH_SSL=static ENABLE_SSPI=no ENABLE_IPV6=no DEBUG=no

参数详细含义如下:

nmake /f Makefile.vc mode=<static or dll> <options>

where <options> is one or many of:
  VC=<6,7,8,9,10,11,12,14>     - VC versions
  WITH_DEVEL=<path>            - Paths for the development files (SSL, zlib, etc.)
                                 Defaults to sibbling directory deps: ../deps
                                 Libraries can be fetched at http://windows.php.net/downloads/php-sdk/deps/
                                 Uncompress them into the deps folder.
  WITH_SSL=<dll or static>     - Enable OpenSSL support, DLL or static
  WITH_CARES=<dll or static>   - Enable c-ares support, DLL or static
  WITH_ZLIB=<dll or static>    - Enable zlib support, DLL or static
  WITH_SSH2=<dll or static>    - Enable libSSH2 support, DLL or static
  ENABLE_SSPI=<yes or no>      - Enable SSPI support, defaults to yes
  ENABLE_IPV6=<yes or no>      - Enable IPv6, defaults to yes
  ENABLE_IDN=<yes or no>       - Enable use of Windows IDN APIs, defaults to yes
                                 Requires Windows Vista or later, or installation from:
                                 http://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815
  ENABLE_WINSSL=<yes or no>    - Enable native Windows SSL support, defaults to yes
  GEN_PDB=<yes or no>          - Generate Program Database (debug symbols for release build)
  DEBUG=<yes or no>            - Debug builds
  MACHINE=<x86 or x64>         - Target architecture (default is x86)
  
  Static linking of Microsoft's C RunTime (CRT):
==============================================
If you are using mode=static nmake will create and link to the static build of
libcurl but *not* the static CRT. If you must you can force nmake to link in
the static CRT by passing RTLIBCFG=static. Typically you shouldn't use that
option, and nmake will default to the DLL CRT. RTLIBCFG is rarely used and
therefore rarely tested. When passing RTLIBCFG for a configuration that was
already built but not with that option, or if the option was specified
differently, you must destroy the build directory containing the configuration
so that nmake can build it from scratch.

Legacy Windows and SSL
======================
When you build curl using the build files in this directory the default SSL
backend will be WinSSL (Windows SSPI, more specifically Schannel), the native
SSL library that comes with the Windows OS. WinSSL in Windows <= XP is not able
to connect to servers that no longer support the legacy handshakes and
algorithms used by those versions. If you will be using curl in one of those
earlier versions of Windows you should choose another SSL backend like OpenSSL.

3.验证

vs2015
添加好包含目录 ,附加库目录, 附加依赖项后,预处理器添加上BUILDING_LIBCURL

// staticLibcurlTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <curl.h>

using namespace std;

/**
* 一旦curl接收到数据,就会调用此回调函数
* buffer:数据缓冲区指针
* size:调试阶段总是发现为1
* nmemb:(memory block)代表此次接受的内存块的长度
* userp:用户自定义的一个参数
*/
size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
{
	static int current_index = 0;

	cout << "current:" << current_index++;
	cout << (char*)buffer;
	cout << "---------------" << endl;

	int temp = *(int*)userp;    // 这里获取用户自定义参数
	return nmemb;
}

int main()
{
	curl_global_init(CURL_GLOBAL_ALL); // 首先全局初始化CURL
	CURL* curl = curl_easy_init(); // 初始化CURL句柄

	if (NULL == curl)
	{
		return 0;
	}

	int my_param = 1;    // 自定义一个用户参数

						 // 设置目标URL
	curl_easy_setopt(curl, CURLOPT_URL, "https://api.vxxx/gettime");
	// 设置接收到HTTP服务器的数据时调用的回调函数
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	// 设置自定义参数(回调函数的第四个参数)
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &my_param);
	// 执行一次URL请求
	CURLcode res = curl_easy_perform(curl);
	// 清理干净
	curl_easy_cleanup(curl);
	getchar();
	return 0;
}

参考:
https://blog.csdn.net/fm0517/article/details/91822880
https://blog.csdn.net/huangyimo/article/details/80337496

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSPI 打造纯API+汇编 winhttp及socket支持SSL安全连接 抛弃累赘的OpenSSL,拥抱windows SSPI ! 背景:这也是在使用当中遇到的坑,winhttp组件在某些电脑上无法使用,要么open卡死,要么send卡死,后面用了hpsocket,封装了iwinhttp(hpsocket <-前面是可以点的哦~)。但是hpsocket也有各种不安逸的地方,httpsync组件读文件在网速很慢的时候要设置超大的超时时间才能全部读取完,不能读写回调,所以就萌生了一个自己打造的念头。 前辈:然而自己论坛也有开源的基于OpenSSL+Socket的WinHttp(anthttp<-前面是可以点的哦~),我看了下,分块没处理,还有就是强制recv返回0的时候才跳出读取循环,这个就有点尴尬了,而且还带两个dll比较麻烦。 SSPI:再来SSPI是什么?SSPI是Security Support Provider Interface(Microsoft安全支持提供器接口)的英文缩写。SSPI是定义得较全面的公用API,用来获得验证、信息完整性、信息隐私等集成安全服务,以及用于所有分布式应用程序协yi的安全方面的服务。 通过科学上网发现这个是可以用来进行SSL加密的,所以潜心研究,在这也感谢心宇和kyozy的指导,因为C的自定义数据类型有些和易语言是不一样的,特别是在数组的情况下.也感谢MSDN上的demo和一些开源作品(curl,sslsocket)。 这次开源的呢算是个半成品,代码也写得比较乱,但是勉强能看懂吧。因为我发现易语言没有任何关于SSPI的demo以及稍微淡到一点的,算是开了个先驱吧。 本次开源的模块实现了http和https访问,分块处理,SSPI的创建、握手、加密、解密(本次开源的重点)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值