{"id":2305,"date":"2016-07-20T11:35:32","date_gmt":"2016-07-20T17:35:32","guid":{"rendered":"http:\/\/162.243.68.163\/?p=2305"},"modified":"2017-05-22T16:40:39","modified_gmt":"2017-05-22T22:40:39","slug":"compiling-static-openssl-with-mdd","status":"publish","type":"post","link":"https:\/\/tekhedd.com\/?p=2305","title":{"rendered":"Compiling static OpenSSL with \/MD[d]"},"content":{"rendered":"<p>So, you have a DLL that uses OpenSSL. Maybe your DLL uses libcurl and you want to support HTTPS? And, you want to statically link to avoid distributing multiple files.<\/p>\n<p>You&#8217;re couldn&#8217;t find a static build of the OpenSSL libs, could you? And when you downloaded the source, you found that only supports static linkage as an afterthought, and when it does, it links with \/MT.<\/p>\n<p>Static MSVCRT. How quaint.<\/p>\n<p>DLLs pretty much always compile with \/MD (and \/MDd in debug mode). And, in fact, it&#8217;s not just DLLs; statically linking with \/MT is just simply a bad idea these days, because you need to update the C runtime libraries often. Your DLL needs static .lib versions of OpenSSL compiled with \/MD (\/MDd for debugging). Those aren&#8217;t provided. And the makefiles won&#8217;t build them. And there are no instructions on how to build them.<\/p>\n<p>Let&#8217;s be honest, statically linking against OpenSSL is not a great idea either. But there are good business reasons for what you&#8217;re doing, and I&#8217;m not going to judge. I&#8217;m writing this because I&#8217;m doing the same thing.<\/p>\n<h3>32-bit build<\/h3>\n<p>Here&#8217;s my build process for a 32-bit debug build, based on openssl-1.0.2h. Open a Visual Studio command prompt for your target version (I&#8217;m using 2010), then run these commands:<\/p>\n<pre style=\"padding-left: 30px;\">perl Configure no-asm debug-VC-WIN32 --prefix=f:\\openssl-32-debug\r\nms\\do_ms\r\n!!!  HACK ms\nt.mak to replace \/MTd with \/MDd: I found only one !!!\r\nnmake -f ms\nt.mak\r\nnmake -f ms\nt.mak install<\/pre>\n<p>You can repeat this for the non-debug target with \/MD, and repeat the whole process in a different directory tree for the 64 bit builds, as shown in other people&#8217;s examples.<\/p>\n<p>Notes:<\/p>\n<ul>\n<li>I couldn&#8217;t get the assembler to compile even with nasm installed, so I just gave up on that (hence &#8220;no-asm&#8221;).<\/li>\n<li>Version 1.0.2a gave me link errors even with a vanilla build, so I think it&#8217;s broken.<\/li>\n<\/ul>\n<h3>64-bit build<\/h3>\n<p>It&#8217;s almost the same. In the 64-bit Visual Studio command prompt:<\/p>\n<pre style=\"padding-left: 30px;\">perl Configure no-asm debug-VC-WIN32 --prefix=f:\\openssl-64-debug\r\nms\\do_win64a\r\n!!!  HACK ms\nt.mak to replace \/MTd with \/MDd as with the 32-bit build !!!\r\nnmake -f ms\nt.mak\r\nnmake -f ms\nt.mak install<\/pre>\n<h3>Bonus Credit<\/h3>\n<p>For bonus points, also run the tests.<\/p>\n<pre style=\"padding-left: 30px;\">nmake -f ms\nt.mak test<\/pre>\n<h3>Extra Bonus: Assembly Code<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/users\/6827240\/daboul\">Daboul<\/a>\u00a0at stackoverflow got the assembly to compile. According to <a href=\"https:\/\/stackoverflow.com\/a\/25972312\/2073670\">his comment on this answer<\/a>, this script will do it:<\/p>\n<pre style=\"padding-left: 30px;\">call cd src\\openssl-1.0.2k \r\ncall \"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat\" \r\ncall set path=C:\\Perl64\\bin;C:\\Users\\m\\AppData\\Local\\NASM;%path% \r\ncall perl Configure VC-WIN32 --prefix=\"..\/..\/build\/1.0.2k-x86_rel_MD\" \r\ncall ms\\do_nasm.bat powershell -Command \"(Get-Content ms\nt.mak).replace('\/MT', '\/MD') | Set-Content ms\nt.mak\" \r\ncall nmake -f ms\nt.mak \r\ncall nmake -f ms\nt.mak install<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, you have a DLL that uses OpenSSL. Maybe your DLL uses libcurl and you want to support HTTPS? And, you want to statically link to avoid distributing multiple files. You&#8217;re couldn&#8217;t find a static build of the OpenSSL libs, could you? And when you downloaded the source, you found that only supports static linkage [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2305","post","type-post","status-publish","format-standard","hentry","category-other"],"_links":{"self":[{"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/posts\/2305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tekhedd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2305"}],"version-history":[{"count":11,"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/posts\/2305\/revisions"}],"predecessor-version":[{"id":2426,"href":"https:\/\/tekhedd.com\/index.php?rest_route=\/wp\/v2\/posts\/2305\/revisions\/2426"}],"wp:attachment":[{"href":"https:\/\/tekhedd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tekhedd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tekhedd.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}