Link against exe on Windows fails - .a file not found

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Link against exe on Windows fails - .a file not found

Michael Ho
Hey there.

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.
clang++.exe: error: no such file or directory: 'ws2editor/libws2editor.dll.a'

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.


After a bit more digging around I came across https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt - 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).

So how can I get the CMake build on Windows to create this file, or otherwise link with an executable?
Thanks.

If you want the full CMake scripts, they're available on GitHub.



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link against exe on Windows fails - .a file not found

Jano Svitok
On Sun, May 13, 2018 at 1:41 PM, Michael Ho <[hidden email]> wrote:

>
> Hey there.
>
> 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.
>
> clang++.exe: error: no such file or directory: 'ws2editor/libws2editor.dll.a'
>
> 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.
>
>
> After a bit more digging around I came across https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt - 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).
>
> So how can I get the CMake build on Windows to create this file, or otherwise link with an executable?

You need to 1. mark some/all symbols to export 2. actually export them
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 https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/, check also comments for possible drawbacks).

https://github.com/Kitware/CMake/blob/master/Tests/Plugin/include/example.h shows how to use __declspec(dllexport): for each dll you define a macro (EXAMPLE_EXPORT 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 (example_exe_function). For class members, it's enough to mark the class.
Finally there's a #define (example_exe_EXPORTS here) that controls how is the macro defined. See https://cmake.org/cmake/help/git-master/prop_tgt/DEFINE_SYMBOL.html (I didn't know this until now, we used to manually define the symbol!)

Jano

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link against exe on Windows fails - .a file not found

Jano Svitok
On Mon, May 14, 2018 at 1:34 AM, Jano Svitok <[hidden email]> wrote:
ENABLE_EXPORTS does #2.

Note: for dlls (aka shared libraries) symbols are enabled automatically. You need ENABLE_EXPORTS 1 only for exes.
Note2: I use MSVC, I have little experience with clang.

Jano

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link against exe on Windows fails - .a file not found

Michael Ho
In reply to this post by Jano Svitok
Ah yes, I forgot Windows doesn't export symbols by default.
Unfortunately adding set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1) didn't seem to help however - I get the same file not found error.

Something interesting I noticed however is that if I copy and rename ws2editor/CMakeFiles/ws2editor.dir/objects.a to ws2editor/libws2editor.dll.a, 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).

Thanks
- Mike

(Note: sorry for the mostly duplicate message - I wasn't aware that you should use "Reply All" in mailing lists)


From: Jano Svitok <[hidden email]>
Sent: 14 May 2018 00:34
To: Michael Ho
Cc: [hidden email]
Subject: Re: [CMake] Link against exe on Windows fails - .a file not found
 
On Sun, May 13, 2018 at 1:41 PM, Michael Ho <[hidden email]> wrote:
>
> Hey there.
>
> 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.
>
> clang++.exe: error: no such file or directory: 'ws2editor/libws2editor.dll.a'
>
> 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.
>
>
> After a bit more digging around I came across https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt - 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).
>
> So how can I get the CMake build on Windows to create this file, or otherwise link with an executable?

You need to 1. mark some/all symbols to export 2. actually export them
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 https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/, check also comments for possible drawbacks).

https://github.com/Kitware/CMake/blob/master/Tests/Plugin/include/example.h shows how to use __declspec(dllexport): for each dll you define a macro (EXAMPLE_EXPORT 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 (example_exe_function). For class members, it's enough to mark the class.
Finally there's a #define (example_exe_EXPORTS here) that controls how is the macro defined. See https://cmake.org/cmake/help/git-master/prop_tgt/DEFINE_SYMBOL.html (I didn't know this until now, we used to manually define the symbol!)

Jano

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link against exe on Windows fails - .a file not found

Michael Ho
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! :)

if(WIN32)
    #Invoke ar to generate a .dll.a from the .obj files, required to link plugins
    add_custom_command(TARGET ${PROJECT_NAME} PRE_LINK
        COMMAND sh -c "${CMAKE_AR} cr lib${PROJECT_NAME}.dll.a $$(find . -name '*.obj' -printf '%p ')"
        COMMENT "Generating lib${PROJECT_NAME}.dll.a for external linking"
        )

    #Also add the install command for libws2editor.dll.a
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.dll.a DESTINATION bin)
endif(WIN32)

-Mike


From: Michael Ho <[hidden email]>
Sent: 18 May 2018 11:55
To: Jano Svitok; Michael Ho
Cc: [hidden email]
Subject: Re: [CMake] Link against exe on Windows fails - .a file not found
 
Ah yes, I forgot Windows doesn't export symbols by default.
Unfortunately adding set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1) didn't seem to help however - I get the same file not found error.

Something interesting I noticed however is that if I copy and rename ws2editor/CMakeFiles/ws2editor.dir/objects.a to ws2editor/libws2editor.dll.a, 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).

Thanks
- Mike

(Note: sorry for the mostly duplicate message - I wasn't aware that you should use "Reply All" in mailing lists)


From: Jano Svitok <[hidden email]>
Sent: 14 May 2018 00:34
To: Michael Ho
Cc: [hidden email]
Subject: Re: [CMake] Link against exe on Windows fails - .a file not found
 
On Sun, May 13, 2018 at 1:41 PM, Michael Ho <[hidden email]> wrote:
>
> Hey there.
>
> 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.
>
> clang++.exe: error: no such file or directory: 'ws2editor/libws2editor.dll.a'
>
> 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.
>
>
> After a bit more digging around I came across https://github.com/Kitware/CMake/blob/master/Tests/Plugin/CMakeLists.txt - 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).
>
> So how can I get the CMake build on Windows to create this file, or otherwise link with an executable?

You need to 1. mark some/all symbols to export 2. actually export them
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 https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/, check also comments for possible drawbacks).

https://github.com/Kitware/CMake/blob/master/Tests/Plugin/include/example.h shows how to use __declspec(dllexport): for each dll you define a macro (EXAMPLE_EXPORT 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 (example_exe_function). For class members, it's enough to mark the class.
Finally there's a #define (example_exe_EXPORTS here) that controls how is the macro defined. See https://cmake.org/cmake/help/git-master/prop_tgt/DEFINE_SYMBOL.html (I didn't know this until now, we used to manually define the symbol!)

Jano

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake