<div dir="ltr"><div>Hi Everyone (particularly Howard and Reid (rnk) and any windows experts out there)</div><div> </div><div>I'm trying to hack some more support for libcxx on Windows (for any compilers) but cl.exe/visual studio in this case:</div>
<div> </div><div>One task is I'm trying to find out what the Windows equivalent is to the gcc/clang weak attribute.</div><div>(would address sanitizer or other llvm related code bases benefit from any solution that can be found here?).</div>
<div> </div><div>Take code like this, from libcxx:</div><div> </div><div>__attribute__((__weak__, __visibility__("default"))) void * operator new(std::size_t size) { ... }</div><div> </div><div>With cl.exe/Visual Studio, this fails to compile.</div>
<div> </div><div>One idea, I'm wondering, is if the above line could be changed to this:</div><div> </div><div><font face="Consolas"><font color="#6f008a"><font color="#6f008a"><font color="#6f008a">_LIBCPP_WEAK</font></font></font><font><font> </font></font><font color="#6f008a"><font color="#6f008a"><font color="#6f008a">_LIBCPP_FUNC_VIS </font></font></font></font><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas">void</font></font></font><font face="Consolas"><font face="Consolas"> *</font></font><font face="Consolas"><font face="Consolas">
 </font></font><font face="Consolas"><font face="Consolas">operator new(std::</font></font><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas">size_t</font></font></font><font face="Consolas"><font face="Consolas"> </font></font><font color="#808080" face="Consolas"><font color="#808080" face="Consolas"><font color="#808080" face="Consolas">size</font></font></font><font face="Consolas"><font face="Consolas">) {...}</font></font></div>
<div> </div><div>* Where _LIBCPP_WEAK could/would be added to libcxx's __config like so (i.e. new code):</div><div>#if defined(_WIN32)<br>#  define _LIBCPP_WEAK __declspec(noinline) __inline <br>#else<br>#  define _LIBCPP_WEAK  __attribute__((weak))<br>
#endif</div><div> </div><div>* And _LIBCPP_FUNC_VIS would be used instead of the existing default visibility attribute.</div><div>__config already defines this as so:</div><div> </div><div>// snipped from existing __config:</div>
<div>#ifdef _WIN32</div><div>// only really useful for a DLL<br>#ifdef _LIBCPP_DLL // this should be a compiler builtin define ideally...<br># ifdef cxx_EXPORTS<br>#  define _LIBCPP_HIDDEN<br>#  define _LIBCPP_FUNC_VIS __declspec(dllexport)<br>
#  define _LIBCPP_TYPE_VIS __declspec(dllexport)<br># else<br>#  define _LIBCPP_HIDDEN<br>#  define _LIBCPP_FUNC_VIS __declspec(dllimport)<br>#  define _LIBCPP_TYPE_VIS __declspec(dllimport)<br># endif<br>#else<br># define _LIBCPP_HIDDEN<br>
# define _LIBCPP_FUNC_VIS<br># define _LIBCPP_TYPE_VIS<br>#endif<br>//.... snip<br>#ifndef _LIBCPP_FUNC_VIS<br>#define _LIBCPP_FUNC_VIS __attribute__ ((__visibility__("default")))<br>#endif</div><div> </div><div>
The thinking is that:</div><div><div><font face="Consolas"><font color="#6f008a"><font color="#6f008a"><font color="#6f008a">_LIBCPP_WEAK</font></font></font><font><font> </font></font><font color="#6f008a"><font color="#6f008a"><font color="#6f008a">_LIBCPP_FUNC_VIS </font></font></font></font><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas">void</font></font></font><font face="Consolas"><font face="Consolas"> *</font></font><font face="Consolas"><font face="Consolas">  </font></font><font face="Consolas"><font face="Consolas">operator new(std::</font></font><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas">size_t</font></font></font><font face="Consolas"><font face="Consolas"> </font></font><font color="#808080" face="Consolas"><font color="#808080" face="Consolas"><font color="#808080" face="Consolas">size</font></font></font><font face="Consolas"><font face="Consolas">) {...}</font></font></div>
</div><div> </div><div>would expand out from the above to this for apple/linux/etc (i.e. remain what it already is on those platforms):</div><div><font face="Consolas"><font color="#6f008a"><font color="#6f008a"><font color="#6f008a"><font face="Arial">__attribute__((weak))</font></font></font></font><font><font> <font face="Arial">__attribute__ ((__visibility__("default")))</font></font></font><font color="#6f008a"><font color="#6f008a"><font color="#6f008a"> </font></font></font></font><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas"><font color="#0000ff" face="Consolas">void</font></font></font><font face="Consolas"><font face="Consolas"> *</font></font><font face="Consolas"><font face="Consolas">  </font></font><font face="Consolas"><font face="Consolas">operator new(std::</font></font><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas"><font color="#2b91af" face="Consolas">size_t</font></font></font><font face="Consolas"><font face="Consolas"> </font></font><font color="#808080" face="Consolas"><font color="#808080" face="Consolas"><font color="#808080" face="Consolas">size</font></font></font><font face="Consolas"><font face="Consolas">) {...}</font></font></div>
<div><font face="Consolas"></font> </div><div><font face="Consolas">But on Windows, it would expand to this:</font><br>__declspec(dllexport) /* or nothing */ __declspec(noinline) <font face="Consolas"><font color="#0000ff">void</font><font><font> *</font></font><font><font>  </font></font><font><font>operator new(std::</font></font><font color="#2b91af"><font color="#2b91af"><font color="#2b91af">size_t</font></font></font><font><font> </font></font><font color="#808080"><font color="#808080"><font color="#808080">size</font></font></font><font><font>) {...}</font></font></font></div>
<div><font face="Consolas"></font> </div><div><font face="Consolas">I got the idea from here (see workarounds):</font></div><div><a href="http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-c-c">http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-c-c</a></div>
<div> </div><div>Alternatively, would this suggestion I found on SO both be correct and legitimate to use?:</div><div><a href="http://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio">http://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio</a></div>
<div>and if so what would the reformulated macros look like using it? (_Pragma?)</div><div> </div><div>Generally the whole inline/visibility situation for libcxx on Windows needs some examination and is subtle to get right it seems.</div>
<div> </div><div>It might be in this example macros aren't the right implementation choice, a straight #if _MSC_VER #else might be better?</div><div> </div><div>But hopefully I've explained the problem I'm trying to solve however it's done. So what to do here for cl.exe?</div>
<div> </div><div>While on the topic, it'd be nice to find the correct solution to this problem too:  </div><font color="#1e1e1e" face="Consolas" size="1"><font color="#1e1e1e" face="Consolas" size="1"><font color="#1e1e1e" face="Consolas" size="1"><p>
2>c:/libcxx/include\utility(597): warning C4141: 'inline' : used more than once</p></font></font></font><div>It's a warning but it would be nice to find the correct formulation of macro's that will solve that warning too.</div>
<div>When libcxx is compiled with cl.exe this warning appears 1300 times for various offending lines </div><div> </div><div>The attached patch demonstrates the first proposal, using libcxx's new.cpp as an example. It at least removes some of the compile errors coming from cl.exe, but it may still not be right even if it compiles.</div>
<div> </div><div>Suggestions please. Thanks for your help.</div></div>