一、环境win7 64位 vs2010
二、文件准备:
2.1文件下载
- libcurl 下载页面: http://curl.haxx.se/download.html (我下载的是https://curl.haxx.se/download/curl-7.50.3.zip)
- openssl 下载页面:http://www.openssl.org/source (我下载的是openssl-1.0.1u)
- ActivePerl(编译openssl时要用) 下载页面 http://www.activestate.com/activeperl/downloads 根据自己的需求选择64位或32位
2.2文件解压及安装
- ActivePerl安装,双击直接下一步,默认设置到底。
- openssl、 libcurl分别解压,将他们放在同一级目录。 例如:“D:\TestCurl\curl-7.50.3”、“D:\TestCurl\openssl-1.0.1u”
三、编译过程:
3.1 openssl编译
- 使用VS2010下的Visual Studio 2010 Command Prompt进入控制台模式 (开始菜单->所有程序->Microsoft->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio 2010 Command Prompt)
- 进入openssl源码的目录
命令行键入:d:
命令行键入:cd D:\TestCurl\openssl-1.0.1u
//把路径替换成自己的源码路径
3.
命令行键入:perl configure VC-WIN32
4.
命令行键入:ms\do_ms.bat
5. 编译分两种情况,生成静态库和动态库
(1) 如果是编译OpenSSL动态库,则在命令行键入:nmake -f ms\ntdll.mak
(编译成功在文件夹out32dll里面生成输出文件,包括应用程序的exe文件、lib文件、dll文件。)
(2) 如果是编译OpenSSL静态库,则在命令行键入:nmake -f ms\nt.mak
(编译成功在文件夹out32里面生成输出文件,包括应用程序的exe文件、lib文件。)
注意:此处编译需要较长时间,请耐心等待,切勿人为中断。
到此编译已经完成,将OpenSSL下的include文件夹、lib文件、dll文件考出,使用的时候包含进去就行了。
另外还有几个命令可能会用到:
测试OpenSSL动态库:nmake -f ms\ntdll.mak test
测试OpenSSL静态库:nmake -f ms\nt.mak test
安装OpenSSL动态库:nmake -f ms\ntdll.mak install
安装OpenSSL静态库:nmake -f ms\nt.mak install
清除上次OpenSSL动态库的编译,以便重新编译:nmake -f ms\ntdll.mak clean
清除上次OpenSSL静态库的编译,以便重新编译:nmake -f ms\nt.mak clean
测试OpenSSL静态库:nmake -f ms\nt.mak test
安装OpenSSL动态库:nmake -f ms\ntdll.mak install
安装OpenSSL静态库:nmake -f ms\nt.mak install
清除上次OpenSSL动态库的编译,以便重新编译:nmake -f ms\ntdll.mak clean
清除上次OpenSSL静态库的编译,以便重新编译:nmake -f ms\nt.mak clean
3.2 libcurl编译
在 “D:\TestCurl\curl-7.50.3</span>/lib” 目录下新建release.bat,
输入以下内容:
call "C:/Program Files/Microsoft Visual Studio 10.0/VC/bin/vcvars32.bat" //路径为vs2010安装目录
set CFG=release-dll-ssl-dll
set OPENSSL_PATH=../../openssl-1.0.1u
nmake -f Makefile.vc10
保存,然后运行。 等待一会儿,就可以到 curl-7.53.0\lib\release-dll-ssl-dll 目录下拷文件 libcurl_imp.lib和libcurl.dll 啦!
四、使用curl库发送https请求
- 打开vs2010新建一个控制台程序
- 设置工程属性
2.1设置附加包含目录路径为 "D:\TestCurl\curl-7.50.3\include" (或者新建一个文件夹将"D:\TestCurl\curl-7.50.3\include"中的文件拷进去,并将该文件夹路径设置为附加包含目录路径.
2.2 设置链接库器的附加库目录路径
新建一个文件夹将编译libcurl生成的libcurl_imp.lib还有编译openssl生成的libeay32.lib,ssleay32.lib拷进去,并将该文件夹路径设置为附加包含目录路径.
2.3 添加链接器的附加库依赖项 ws2_32.lib、wldap32.lib
2.4添加dll :在输出可执行文件*.exe的路径中添加编译libcurl生成的libcurl.dll以及编译openssl生成的libeay32.dll、ssleay32.dll.
3.代码
#include "stdafx.h"
#include <curl/curl.h>
#pragma comment(lib,"libcurl.lib")
int _tmain(int argc, _TCHAR* argv[])
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true);
curl_easy_setopt(curl, CURLOPT_URL, "https://mail.qq.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* 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);
}
return 0;
}4.结果
本文详细介绍了如何在Win7 64位系统、Visual Studio 2010环境下编译OpenSSL和cURL库,以实现c++程序中发送HTTPS请求。步骤包括下载所需文件、配置编译环境、编译OpenSSL动态库和静态库,以及编译cURL库。最后,文章提到了创建新的控制台程序并设置工程属性以使用编译好的库进行HTTPS请求。
1736

被折叠的 条评论
为什么被折叠?



