Libcurl, a worldwide well-known internet downloader, integrated with PHP and much more. Its executable is pre-installed in hundreds of thousands of Linux distributions.
Let's bring it to Windows with the best and the most incompatible compiler: MSVC. In the end of the article, what you get is a fully functional, completely dependency-free libcurl that runs across all Windows versions all the way back to XP.
1) Prepare sources.
First things first, download all the sources required.
For today's project, we want our final libcurl to support tls1.2 (may be tls1.3), gzip, deflate and brotli. All functions are statically linked, no runtime whatsoever is needed to end-users.
Here is a list of sources and websites involved.
It's a long list...
- libcurl https://curl.haxx.se/download/curl-7.68.0.tar.gz
- zlib https://www.zlib.net/zlib-1.2.11.tar.gz
- nghttp2 https://github.com/nghttp2/nghttp2/archive/master.zip
- brotli https://github.com/google/brotli/archive/master.zip or just git clone it.
- openssl https://www.openssl.org/source/openssl-1.1.1d.tar.gz
- CMake https://github.com/Kitware/CMake/releases/download/v3.17.0-rc1/cmake-3.17.0-rc1-win64-x64.msi
- strawberry perl http://strawberryperl.com/download/5.30.1.1/strawberry-perl-5.30.1.1-64bit.msi or Activeperl add packages to platform and build, it's a pain, highly not recommended.
- nasm http://www.nasm.us/pub/nasm/releasebuilds/2.14.02/ or disable it in openssl.
- Microsoft Visual studio. 😜
2) Install
Install all the tools.
Unpack all the archives.
Let's assume the following:
openssl at D:\openssl
zlib at D:\zlib
broli at D:\brotli
libcurl at D:\libcurl
nghttp2 at D:\nghttp2
and open a msvc command line windows for our job. (starts from start menu->Developer Command Prompt for VS20XX orVsDevCmd.bat
in vs tools folder)
3) Build OpenSSL
Openssl used to be the toughest one to compile, it's way better now.
In the cmd windows we opened, run perl Configure VC-WIN32 --prefix=D:\openssl\builds no-shared
to config it.
Run nmake /E CC="cl /D_USING_V110_SDK71_ /D_WIN32_WINNT=0x0501" /f makefile
to compile with nmake and v150_xp toolset which is compatible with xp. Or just nmake
to compile if xp users is not your thing. (Who cares about them anyways)
Run nmake install
to finish the compiling.
If everything is installed correctly, no error should occur, and 4 folders bin
, html
, include
and lib
should laying there in D:\openssl\builds. And two static libraries libssl.lib
and libcrypto.lib
should be big enough to indicate that they are not a ref to dlls.
4) Build zlib
Not as easy as you thought. Just don't use the provided solution file.
Open win32\Makefile.msc
and change CFLAGS ..... -MD
to CFLAGS ... -MT
cd to D:\zlib in the cmd windows left, and run nmake /E CC="cl /D_USING_V110_SDK71_ /D_WIN32_WINNT=0x0501" /f win32/Makefile.msc
An zlib.lib
should sit in the same folder when compiling ends.
5) Build Brotli
Head over to D:\brotli and do:
mkdir out-static && cd out-static
cmake .. -DBUILD_SHARED_LIBS=OFF
Wait a few seconds and there should a brotli.sln
solution file and a bunch of vc projects waiting for you.
Open the solution and configure it.
Only the ones ended with -static
are needed. Or vice versa if you're happy with dlls.
After compiling, three libs with static in their names are expected.
6) Build nghttp2
Ignore it if you don't need http2 support.
cd to D:\nghttp2
open cmake-gui
browse source to the nghttp2 folder
browse build to the location where you want your code to be, d:\nghttp2\builds for example.
Do configure
, and select the vs version you would like to configure to.
configure the settings appeared in the middle of the box
CMAKE_CONFIGURATION_TYPES = Release
ENABLE_STATIC_LIB = true
Do generate
and Open project
. MSVC will be launched.
Do the regular configure, remember, it is default to /MD.
Compile project nghttp2-static
. All the other projects are not required for libcurl.
7) Build libcurl
Finally, we made it here.
Go to D:\libcurl\projects\windows\{your-vc-ver}
or D:\libcurl\projects\windows\{your-vc-ver}\lib
if libcurl is the only thing you're interested, and open it.
Configure it to use /MT
and xp toolset, you should be quite good at it at this point.
Add include folders of all your previously built dependencies to it. In this case:
D:\openssl\builds\include
D:\zlib
D:\brotli\c\include
D:\nghttp2\build\lib\includes
D:\nghttp2\lib\includes (Yes, you need both of them)
and libraries
D:\openssl\builds\lib
D:\brotli\out-static\Release
D:\zlib
D:\nghttp2\build\lib\Release
Add
libssl.lib
libcrypto.lib
CRYPT32.LIB
zlib.lib
brotlidec-static.lib
brotlienc-static.lib
brotlicommon-static.lib
nghttp2.lib
to the linker option.
If there is libeay32.lib
and ssleay32.lib
, remove them. They are for old versions of openssl.
Add
HAVE_LIBZ
HAVE_ZLIB_H
HAVE_BROTLI
USE_NGHTTP2
NGHTTP2_STATICLIB
as preprocessor definitions
Hit build.
Congrats, nothing could stop you from enjoying your libcurl now.
Thanks to:
https://github.com/google/brotli/issues/543#issuecomment-329652256
https://albertino80.github.io/building.html
https://stackoverflow.com/questions/15928293/vs2012-nmake-using-v110-xp-toolset
https://en.delphipraxis.net/topic/986-win-xp-app-fails-to-start-missing-bcryptdll/
https://github.com/openssl/openssl/issues/1061
http://p-nand-q.com/programming/windows/building_openssl_with_visual_studio_2013.html
https://www.cnblogs.com/leehm/p/12066683.html
https://www.geek-share.com/detail/2581783821.html
0 comment