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 versionsWITH_DEVEL=<path> - Paths for the development files (SSL, zlib, etc.)Defaults to sibbling directory deps: ../depsLibraries 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 staticWITH_CARES=<dll or static> - Enable c-ares support, DLL or staticWITH_ZLIB=<dll or static> - Enable zlib support, DLL or staticWITH_SSH2=<dll or static> - Enable libSSH2 support, DLL or staticENABLE_SSPI=<yes or no> - Enable SSPI support, defaults to yesENABLE_IPV6=<yes or no> - Enable IPv6, defaults to yesENABLE_IDN=<yes or no> - Enable use of Windows IDN APIs, defaults to yesRequires Windows Vista or later, or installation from:http://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815ENABLE_WINSSL=<yes or no> - Enable native Windows SSL support, defaults to yesGEN_PDB=<yes or no> - Generate Program Database (debug symbols for release build)DEBUG=<yes or no> - Debug buildsMACHINE=<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); // 首先全局初始化CURLCURL* curl = curl_easy_init(); // 初始化CURL句柄if (NULL == curl){return 0;}int my_param = 1; // 自定義一個用戶參數// 設置目標URLcurl_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