Compiling OpenVPN on Windows

OpenVPN is a great project.  It's a shame that their Windows compilation is this painful.  After spending hours trying to compile OpenVPN 2.1_rc7 on Windows using Visual C++, I thought I'd document the process for the benefit of others, and for my own future reference.

Prerequisites:

Build steps:

Extract all of the source packages somewhere in their own directories.  I assume that you're extracting everything in C:\.

Compile OpenSSL

The following steps should be sufficient:

  1. cd C:\openssl-0.9.8g
  2. perl Configure VC-WIN32 --prefix=c:/openssl
  3. ms\do_masm
  4. nmake -f ms\nt.mak &amp;&amp; nmake -f ms\nt.mak test &amp;<span style="font-family: Courier New;">&amp; nmake -f ms\nt.mak install</span>

This will create the OpenSSL binaries as well as header and libraries needed to compile other applications against OpenSSL in C:\openssl.

Compile LZO

Compiling LZO is significantly straightforward:

  1. <span style="font-family: Courier New;">cd C:</span>\lzo-2.03
  2. b\win3<span style="font-family: Courier New;">2\vc.bat</span>

Compile OpenVPN

This should be easy according to the OpenVPN documentation.  We're only supposed to edit makefile.w32-vc, to adjust the path to our OpenSSL and LZO directories, and then run nmake -f makefile.w32-vc.  But doing so would result in a stream of compiler errors:

  1. C:\openvpn-2.1_rc7&gt;nmake -f makefile.w32-vc<p>Microsoft (R) Program Maintenance Utility Version 9.00.21022.08<br />Copyright (C) Microsoft Corporation.  All rights reserved.</p><p>cl.exe @C:\Users\Ehsan\AppData\Local\Temp\nm4DF1.tmp<br />cl : Command line warning D9002 : ignoring unknown option '/G5'<br />base64.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />buffer.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />crypto.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />cryptoapi.c<br />cryptoapi.c(104) : warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details.<br />        C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\string.h(207) : see declaration of 'strdup'<br />error.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />event.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />fdmisc.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />forward.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />fragment.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />gremlin.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />helper.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />init.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />interval.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />list.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />lzo.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />manage.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />mbuf.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />misc.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />mroute.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />mss.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />Generating Code...<br />Compiling...<br />mtcp.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />mtu.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />mudp.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />multi.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />ntlm.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />occ.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />pkcs11.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />openvpn.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />options.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />otime.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />packet_id.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />perf.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />ping.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />plugin.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />pool.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />proto.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />proxy.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />push.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />reliable.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />route.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />Generating Code...<br />Compiling...<br />schedule.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />session_id.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />shaper.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />sig.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />socket.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />socks.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />ssl.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />status.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />thread.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />tun.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />lladdr.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />win32.c<br />c:\openvpn-2.1_rc7\config-win32.h(38) : fatal error C1083: Cannot open include file: 'autodefs/defs.h': No such file or directory<br />Generating Code...<br />NMAKE : fatal error U1077: '&quot;C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe&quot;' : return code '0x2'<br />Stop.</p>

The cause of these errors is line 38 in config-win32.h, which is included in OpenVPN's source package:

  1. #include &quot;autodefs/defs.h&quot;

The autodefs/defs.h file does not exist in the source distribution, hence the compiler errors cited above.  Upon further investigation, it seems like config-win32.h is supposed to be automatically generated from config-win32.h.in, by replacing @VERSION@ with OpenVPN's current version.  I tried taking a diff between the two files, and I noted that several places in the two files are different.  It seems to me that config-win32.h is being manually maintained in the project, and the autodefs/defs.h file has been excluded from the source distribution by mistake.  So, what to do now?  After a small amount of trial and error, I came up with a patch which enables hassle-free compilation of OpenVPN on windows by the promised nmake -f makefile.w32-vc command.  With this patch applied, the compiler errors can be reduced to:

  1. C:\openvpn-2.1_rc7&gt;nmake -f makefile.w32-vc<p>Microsoft (R) Program Maintenance Utility Version 9.00.21022.08<br />Copyright (C) Microsoft Corporation.  All rights reserved.</p><p>cl.exe @C:\Users\Ehsan\AppData\Local\Temp\nm66BE.tmp<br />cl : Command line warning D9002 : ignoring unknown option '/G5'<br />pkcs11.c<br />pkcs11.c(35) : fatal error C1083: Cannot open include file: 'pkcs11-helper-1.0/pkcs11h-certificate.h': No such file or directory<br />NMAKE : fatal error U1077: '&quot;C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe&quot;' : return code '0x2'<br />Stop.</p>

It seems like OpenVPN depends on another library (and this is not documented!): pkcs11-helper.  Their latest package as of this writing can be found here.  You should download and extract this library, and edit makefile.w32-vc to point to the path where you've extracted this library.  Now, you should compile pkcs11-helper.

Compile pkcs11-helper

You can use the following commands to build pkcs11-helper.

  1. cd C:\pkcs11-helper
  2. cd lib
  3. nmake -f Makefile.w32-vc OPENSSL=1 OPENSSL_HOME=C:\openssl-0.9.8g

Note that OPENSSL_HOME should point to the directory where the source files for OpenSSL had been extracted, not the directory where binaries have been installed.

Back to compiling OpenVPN

Now that pkcs11-helper has been built, running nmake -f makefile.w32-vc will succeed, and will produce the OpenVPN binary.  See below if you encounter more errors.

Final notes

At the end, I got some errors from the linker regarding not found libraries.  I had to adjust the names and paths of some libraries in order to make the linker happy.  Some of these changes were expected (for example, I used LZO 2 instead of LZO 1, which uses the new library name lzo2.lib instead of lzo.lib) and some others may be due to changes in the build parameters of the dependencies.

Also, if you get errors regarding multiple defined symbols from the linker in MSVCRT.lib, your dependencies may be mixing the /MD and /MT compiler options.  Make sure you're using the same option in the makefiles for all of the dependencies, as well as OpenVPN itself.  For example, I used /MT every where to make the final OpenVPN binary not depend on the Visual C++ runtime library.

Update

After I managed to compile OpenVPN using the above instructions, someone on the OpenVPN users list mentioned that the Windows build procedure has been changed since 2.1_rc4.  I'm not sure why this is not documented anywhere I looked, but the interested readers can check out the new instructions.  I have not tried them myself, but they look promising!

Trackback URL for this post:

http://ehsanakhgari.org/trackback/4

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

doesn't compile in MS VC 9 Express

SSL doesn't compile MS VC 9 Express, because ml.exe (the assembler compiler) is not included in the free MS Visual C Express Edition :-(

Mabe using the precompiler SSL could work.

Re: doesn't compile in MS VC 9 Express

Yes, I think you can compile non-assembly based source code of OpenSSL.  OpenSSL can be configured not to use optimized assembly-based routines during the compilation stage.  The resulting library is a bit slower, but at least it works for your situation.

Thanks a lot for your article about OpenVpn Compilation

salam aqa ehsan
az rahnamayi hayi besyar mofid shoma dar in post besyar motshakeram , vaqean ali bud.
shoma komake shayani be pishborde yeki az mohemtarin marahele projeam nemudid ...
barayetan doaye kheir mikonam , omidvaram hamishe Movafaq o Sarboland bashid.
;)

Re: Thanks a lot for your article about OpenVpn Compilation

خوشحالم که نوشته‌ام توانست به شما کمک کند.  موفق باشید!

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.