<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Looks like I can use ar to generate the .a file for me, regardless of generator - plugins seem to link and load just fine when doing this. Awesome! :)</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
</div>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);">if(WIN32)</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> #Invoke ar to generate a .dll.a from the .obj files, required to link plugins</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> add_custom_command(TARGET ${PROJECT_NAME} PRE_LINK</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> COMMAND sh -c "${CMAKE_AR} cr lib${PROJECT_NAME}.dll.a $$(find . -name '*.obj' -printf '%p ')"</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> COMMENT "Generating lib${PROJECT_NAME}.dll.a for external linking"</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> )</span><br>
<br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> #Also add the install command for libws2editor.dll.a</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);"> install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.dll.a DESTINATION bin)</span><br>
<span style="font-size: 12pt; color: rgb(0, 0, 0); font-family: "Lucida Console", Monaco, monospace; background-color: rgb(204, 204, 204);">endif(WIN32)</span><br>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-Mike<br>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Michael Ho <CraftedCart@outlook.com><br>
<b>Sent:</b> 18 May 2018 11:55<br>
<b>To:</b> Jano Svitok; Michael Ho<br>
<b>Cc:</b> cmake@cmake.org<br>
<b>Subject:</b> Re: [CMake] Link against exe on Windows fails - .a file not found</font>
<div> </div>
</div>
<meta content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt">Ah yes, I forgot Windows doesn't export symbols by default.<br>
</span></font></div>
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt">Unfortunately adding
<font face="Lucida Console,Monaco,monospace"><span style="background-color:#CCCCCC">set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1)</span></font> didn't seem to help however - I get the same file not found error.</span></font></div>
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt"><br>
</span></font></div>
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt">Something interesting I noticed however is that if I copy and rename
<font face="Courier New,monospace"><span style="background-color:#CCCCCC">w</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">s</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">2</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">e</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">d</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">i</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">t</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">o</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">r</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">/</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">C</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">M</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">a</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">k</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">e</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">F</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">iles</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">/ws2editor.</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">dir/</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">objects.</span></font><font face="Courier New,monospace"><span style="background-color:#CCCCCC">a</span></font>
to <font face="Courier New,monospace"><span style="background-color:#CCCCCC">ws2editor/libws2editor.dll.a</span></font>, the plugin links just fine and even works when I run the application. I suppose I could add this as some post build step, but it certainly
feels like a bit of a hacky solution. This only seems to work for the Unix Makefiles generate however (Which should be fine, although I had been using ninja at the moment so certainly not ideal).<br>
</span></font></div>
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt"><br>
</span></font></div>
<div><font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt">Thanks</span></font></div>
<font size="3" face="Calibri,Helvetica,sans-serif" color="black"><span style="font-size:12pt">- Mike</span></font></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
(Note: sorry for the mostly duplicate message - I wasn't aware that you should use "Reply All" in mailing lists)<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Jano Svitok <jan.svitok@gmail.com><br>
<b>Sent:</b> 14 May 2018 00:34<br>
<b>To:</b> Michael Ho<br>
<b>Cc:</b> cmake@cmake.org<br>
<b>Subject:</b> Re: [CMake] Link against exe on Windows fails - .a file not found</font>
<div> </div>
</div>
<meta content="text/html; charset=utf-8">
<div>
<div dir="ltr">On Sun, May 13, 2018 at 1:41 PM, Michael Ho <<a href="mailto:CraftedCart@outlook.com">CraftedCart@outlook.com</a>> wrote:<br>
><br>
> Hey there.<br>
><br>
> I'm trying to make a plugin system for my application, and thus each plugin will need to link with my main application. This is fine on Linux and macOS, as I can just set the ENABLE_EXPORTS property of my executable target (set_target_properties(${PROJECT_NAME}
PROPERTIES ENABLE_EXPORTS 1), and plugins will link just fine. On Windows however, clang++ yells at me that it can't find libws2editor.dll.a.<br>
><br>
> clang++.exe: error: no such file or directory: 'ws2editor/libws2editor.dll.a'<br>
><br>
> Upon looking over the CMake docs, it says "For DLL platforms an import library will be created for the exported symbols and then used for linking." - This import library file (presumably the .dll.a) never seems to be created though.<br>
><br>
><br>
> After a bit more digging around I came across <a href="https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt">
https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt</a> - When I clone the CMake repo and try and compile that test myself, it also fails with the same error when using clang + ninja! When building with MSVC/msbuild however it compiles
just fine (My project fails to configure when generating files for MSVC however, and ideally I'd like to use the same build config across Win/Mac/Linux).<br>
><br>
> So how can I get the CMake build on Windows to create this file, or otherwise link with an executable?<br>
<br>
You need to 1. mark some/all symbols to export 2. actually export them<br>
<div>ENABLE_EXPORTS does #2. For #1, you need to either mark each symbol to export as __declspec(dllexport) or mark all symbols with WINDOWS_EXPORT_ALL_SYMBOLS (see
<a href="https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/">
https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/</a>, check also comments for possible drawbacks).</div>
<div><br>
</div>
<div><a href="https://github.com/Kitware/CMake/blob/master/Tests/Plugin/include/example.h">https://github.com/Kitware/CMake/blob/master/Tests/Plugin/include/example.h</a> shows how to use __declspec(dllexport): for each dll you define a macro (<span class="x_x_gmail-pl-en">EXAMPLE_EXPORT</span>
here) that becomes either export (when #included from dll) or import (when #included from code linked to dll). Every class/function/variable you want to use from the plugin must be marked this way (<span class="x_x_gmail-pl-smi">example_exe_function</span>).
For class members, it's enough to mark the class.</div>
<div>Finally there's a #define (example_exe_EXPORTS here) that controls how is the macro defined. See
<a href="https://cmake.org/cmake/help/git-master/prop_tgt/DEFINE_SYMBOL.html">https://cmake.org/cmake/help/git-master/prop_tgt/DEFINE_SYMBOL.html</a> (I didn't know this until now, we used to manually define the symbol!)<br>
</div>
<div><br>
</div>
<div>Jano<br>
</div>
</div>
</div>
</div>
</body>
</html>