Boost C++ Libraries: Ticket #6644: Windows: auto_link.hpp incorrectly errors with "Mixing a dll boost library with a static runtime is a really bad idea..." https://svn.boost.org/trac10/ticket/6644 <p> I'm building a DLL which uses *static* runtime linking. Static runtime linking uses the /MT (and /MTd) switch. When Boost is compiled with under the DLL, the following error is reported: </p> <p> C:\Users\Public\boost-trunk\boost/config/auto_link.hpp(354) : fatal error C1189: #error : "Mixing a dll boost library with a static runtime is a really bad idea..." </p> <p> According to "/MD, /MT, /LD (Use Run-Time Library)", <a class="ext-link" href="http://msdn.microsoft.com/en-US/library/2kzt1wy3(v=vs.90).aspx"><span class="icon">​</span>http://msdn.microsoft.com/en-US/library/2kzt1wy3(v=vs.90).aspx</a>: </p> <p> /MT - Causes your application to use the multithread, static version of the run-time library. Defines _MT and causes the compiler to place the library name LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to resolve external symbols. </p> <p> For completeness, here is the switch for dynamic runtime linking: </p> <p> /MD - Causes your application to use the multithread- and DLL-specific version of the run-time library. Defines _MT and _DLL and causes the compiler to place the library name MSVCRT.lib into the .obj file. ... Applications compiled with this option are statically linked to MSVCRT.lib. This library provides a layer of code that allows the linker to resolve external references. The actual working code is contained in MSVCR90.DLL, which must be available at run time to applications linked with MSVCRT.lib... </p> <p> To summarize, "_MT" is defined for both static and dynamic linking. "_DLL" is defined for just dynamic linking. And because I am building a DLL, "_WINDLL" is also defined. </p> <p> Perhaps the following would be better logic for auto_link.hpp (my apologies for not trying to figure out all the Boost defines): </p> <p> #if defined(BOOST_OS_WINDOWS) &amp;&amp; defined(_MT) &amp;&amp; !defined(_DLL) # define BOOST_OS_WINDOWS_STATIC 1 #elif defined(BOOST_OS_WINDOWS) &amp;&amp; defined(_MT) &amp;&amp; defined(_DLL) # define BOOST_OS_WINDOWS_DYNAMIC 1 #elif defined(BOOST_OS_WINDOWS) # pragma warning("Neither static nor dynamic runtime linking has been picked up") #endif </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6644 Trac 1.4.3 noloader@… Thu, 01 Mar 2012 23:48:14 GMT attachment set https://svn.boost.org/trac10/ticket/6644 https://svn.boost.org/trac10/ticket/6644 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">static-dynamic.txt</span> </li> </ul> <p> Various settings for EXE, DLL, staic and dynamic runtime linking </p> Ticket noloader@… Thu, 01 Mar 2012 23:51:20 GMT <link>https://svn.boost.org/trac10/ticket/6644#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:1</guid> <description> <p> Second try with sample defines. </p> <pre class="wiki">#if defined(BOOST_OS_WINDOWS) &amp;&amp; defined(_MT) &amp;&amp; !defined(_DLL) # define BOOST_OS_WINDOWS_STATIC 1 #elif defined(BOOST_OS_WINDOWS) &amp;&amp; defined(_MT) &amp;&amp; defined(_DLL) # define BOOST_OS_WINDOWS_DYNAMIC 1 #elif defined(BOOST_OS_WINDOWS) # pragma warning("Neither static nor dynamic runtime linking has been picked up") #endif </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 02 Mar 2012 01:08:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:2</guid> <description> <p> Forgot to add.... </p> <p> When I build a static LIB (not DLL) using static runtime linking, Boost compiles correctly. So I'm not sure where the leap is made that "I'm building a DLL (with static linking), so I must want to use Boost as a DLL" </p> </description> <category>Ticket</category> </item> <item> <author>noloader@…</author> <pubDate>Fri, 02 Mar 2012 02:44:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:3</guid> <description> <p> Undefining BOOST_DYN_LINK in my project - no joy. </p> </description> <category>Ticket</category> </item> <item> <author>noloader@…</author> <pubDate>Fri, 02 Mar 2012 03:22:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:4</guid> <description> <pre class="wiki">// // auto_link.hpp // select linkage opt: // #if (defined(_DLL) || defined(_RTLDLL)) &amp;&amp; defined(BOOST_DYN_LINK) # define BOOST_LIB_PREFIX #elif defined(BOOST_DYN_LINK) # error "Mixing a dll boost library with a static runtime is a really bad idea..." #else # define BOOST_LIB_PREFIX "lib" #endif </pre><p> The above appears broken. I just verified <code>_DLL</code> is *not* defined. Additionally, I did *not* define <code>BOOST_DYN_LINK</code>. I don't know about <code>_RTLDLL</code>, but I did not define it either. </p> <p> Finally, there does not appear to be a <code>BOOST_STATIC_LINK</code> that I can define. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 02 Mar 2012 13:30:35 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/6644#comment:5 https://svn.boost.org/trac10/ticket/6644#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> <p> I suspect you have BOOST_DYN_LINK defined somewhere in your project settings (or those inherited by your project which can be hard to track down). Take a look at the command line tab in the IDE to see what's set. </p> <p> For me I have: </p> <pre class="wiki">/I"M:\data\boost\trunk" /ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /Gm /EHsc /RTC1 /MTd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\dll_test.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue </pre><p> And building the following as a DLL: </p> <pre class="wiki"> #include "stdafx.h" #define BOOST_LIB_DIAGNOSTIC #include &lt;boost/regex.hpp&gt; void f() { boost::regex e("abc"); } </pre><p> Builds just fine and links to: libboost_regex-vc100-mt-sgd-1_49.lib. </p> <p> If you have a self contained test case (including the IDE project files) that reproduces please reopen, but please check the actual command line passed to the compiler first.... Oh and double check that boost/config/user.hpp hasn't been modified. </p> Ticket anonymous Sat, 03 Mar 2012 18:52:46 GMT <link>https://svn.boost.org/trac10/ticket/6644#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6644#comment:5" title="Comment 5">johnmaddock</a>: </p> <blockquote class="citation"> <p> I suspect you have BOOST_DYN_LINK defined somewhere in your project settings (or those inherited by your project which can be hard to track down). Take a look at the command line tab in the IDE to see what's set. </p> </blockquote> <p> Actually, its not defined by me. Below is a search of the term. It only shows up in Boost gear. </p> <p> Find all "BOOST_DYN_LINK", Subfolders, Find Results 1, "Entire Solution" </p> <blockquote> <p> C:\Documents and Settings\All Users\boost-trunk\boost\config\auto_link.hpp(23):BOOST_DYN_LINK: Optional: when set link to dll rather than static library. </p> <table class="wiki"> <tr>C:\Documents and Settings\All Users\boost-trunk\boost\config\auto_link.hpp(346):#if (defined(_DLL) <td> defined(_RTLDLL)) &amp;&amp; defined(BOOST_DYN_LINK) </td></tr></table> <p> C:\Documents and Settings\All Users\boost-trunk\boost\config\auto_link.hpp(348):#elif defined(BOOST_DYN_LINK) C:\Documents and Settings\All Users\boost-trunk\boost\config\auto_link.hpp(414):#if defined(BOOST_DYN_LINK) C:\Documents and Settings\All Users\boost-trunk\boost\config\auto_link.hpp(415):# undef BOOST_DYN_LINK Matching lines: 5 Matching files: 1 Total files searched: 233 </p> </blockquote> </description> <category>Ticket</category> </item> <item> <author>noloader@…</author> <pubDate>Sat, 03 Mar 2012 19:09:12 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/6644 https://svn.boost.org/trac10/ticket/6644 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">BOOST_DYN_LINK.png</span> </li> </ul> <p> Boost is defining BOOST_DYN_LINK somewhere </p> Ticket anonymous Sat, 03 Mar 2012 19:14:28 GMT <link>https://svn.boost.org/trac10/ticket/6644#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6644#comment:5" title="Comment 5">johnmaddock</a>: </p> <blockquote class="citation"> <p> I suspect you have BOOST_DYN_LINK defined somewhere in your project settings </p> </blockquote> <p> See attached. Boost is manipulating (or doing something with) BOOST_DYN_LINK in at least 30 files. </p> <blockquote class="citation"> <p> Take a look at the command line tab in the IDE to see what's set. </p> </blockquote> <p> As requested (I removed the list of file names): </p> <p> cl /Od /I "../esapi" /I "../errors" /I "../reference" /I "../deps" /I "C:\Documents and Settings\All Users" /I "C:\Documents and Settings\All Users\boost-trunk" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" /D "_STATIC_CPPLIB" /D "ESAPI_MS_DLL_EXPORTS" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MTd /Fo"C:\Documents and Settings\Jeffrey Walton\Desktop\owasp-esapi-c++\VS2008<br />Esapi-Windows\Temp\Debug\x86\esapi-dll<br />" /Fd"C:\Documents and Settings\Jeffrey Walton\Desktop\owasp-esapi-c++\VS2008<br />Esapi-Windows\Temp\Debug\x86\esapi-dll\vc90.pdb" </p> <p> This appears to be a Boost configuration problem. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sat, 03 Mar 2012 19:44:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:8</guid> <description> <p> BOOST_DYN_LINK is an internal macro used by the auto link machinery. The user level macros are BOOST_ALL_DYN_LINK and BOOST_&lt;library name&gt;_DYN_LINK. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 07 Mar 2012 09:19:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:9</guid> <description> <p> Using your command line and the test program I posted above the output message I see is: </p> <p> <code>Linking to lib file: libboost_regex-vc100-mt-sgd-1_49.lib</code> </p> <p> which is the static regex lib as expected/required. </p> <p> As Steven has correctly pointed out (my bad) BOOST_DYN_LINK is an internal macro set just before including the auto-link machinery. For regex it is only set if BOOST_REGEX_DYN_LINK or BOOST_ALL_DYN_LINK are defined: these are user settable macros, we don't set them. </p> <p> Try adding: </p> <pre class="wiki">#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_REGEX_DYN_LINK) #error "Oops" #endif </pre><p> Right before you include boost.regex in your code, my guess is the error will be triggered, and then you just have to figure out where those defines are coming from. </p> <p> HTH, John. </p> </description> <category>Ticket</category> </item> <item> <author>george@…</author> <pubDate>Fri, 16 Nov 2012 21:31:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:10</guid> <description> <p> #define BOOST_PYTHON_STATIC_LIB before including &lt;boost/python.hpp&gt; in your project. Default for Python lib is dynamic linking. I'm using Boost 1.52. </p> </description> <category>Ticket</category> </item> <item> <author>darthpjb@…</author> <pubDate>Sun, 05 Jul 2015 03:50:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6644#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6644#comment:11</guid> <description> <p> I can both confirm that this issue is still present - and also that it is still caused by boost-python. If BOOST_PYTHON_STATIC_LIB is not defined, python defines BOOST_DYN_LINK - and prevents static linking of all boost libraries. </p> </description> <category>Ticket</category> </item> <item> <author>darthpjb@…</author> <pubDate>Sun, 05 Jul 2015 04:04:18 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/6644#comment:12 https://svn.boost.org/trac10/ticket/6644#comment:12 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">worksforme</span> </li> </ul> <p> Apologies for the double-post, but this issue-tracker seems to be having a few problems at the moment. </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/6644#comment:11" title="Comment 11">darthpjb@…</a>: </p> <blockquote class="citation"> <p> I can both confirm that this issue is still present - and also that it is still caused by boost-python. If BOOST_PYTHON_STATIC_LIB is not defined, python defines BOOST_DYN_LINK - and prevents static linking of all boost libraries. </p> </blockquote> <p> This appears when attempting to static-link boost v1.58 into a project using MSVC-12.0, using the /MT /MTD runtime-options. </p> <p> without knowing that BOOST_PYTHON_STATIC_LIB must be defined, this prevents the use of boost in this scenario. </p> <p> Unless there is some reason why boost-python itself cannot be statically linked (which doesn't appear to be the case), this behavior is not only highly-problematic, but potentially breaking. </p> Ticket John Maddock Tue, 07 Jul 2015 10:54:42 GMT owner, status, component changed https://svn.boost.org/trac10/ticket/6644#comment:13 https://svn.boost.org/trac10/ticket/6644#comment:13 <ul> <li><strong>owner</strong> changed from <span class="trac-author">John Maddock</span> to <span class="trac-author">Ralf W. Grosse-Kunstleve</span> </li> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">new</span> </li> <li><strong>component</strong> <span class="trac-field-old">config</span> → <span class="trac-field-new">Python</span> </li> </ul> <p> I don't have Python development files to test with, but if python.hpp is setting these macros then that's a bug.... as is dynamic linking when using the static runtime. </p> <p> Reassigning. </p> Ticket