[CMake] Warning when building static & shared library (DLL) on Windows

Kim Walisch kim.walisch at gmail.com
Sun Sep 30 06:12:27 EDT 2018


Hi,

I have recently found out about CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS which
allows
to easily build a shared library i.e. DLL on Windows. It works great and it
is an awesome
feature however I have run into a warning on Windows (using cmake, ninja,
MSVC) because
my CMakeLists.txt builds both a static and shared library.

Here is what my CMakeLists.txt does:

if(WIN32)
        set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

add_library(mylib SHARED ${LIB_SRC})
...
add_library(mylib STATIC ${LIB_SRC})
...

This works without any issues on all Unix-like OSes. However on Windows
with MSVC I found
out that building the shared library creates both mylib.dll and mylib.lib.
And building the static
library also creates mylib.lib which causes ninja to issue the following
warning:

ninja: warning: multiple rules generate mylib.lib. builds involving this
target will not be correct; continuing anyway [-w dupbuild=warn]

So my question is how to best deal with this warning:

1) Should I ignore it?

2) Should I use add_library(... MODULE ...) instead of SHARED on Windows?
The ninja
maintainer's have suggested this when another user reported the issue here:
https://github.com/ninja-build/ninja/issues/1128#issuecomment-207058115

One issue I see with this approach is that apparently when building a
shared library using
add_library(... MODULE ...) one should avoid
using target_link_libraries(binary mylib)
as mentioned in this stackoverflow answer:
https://stackoverflow.com/a/4968940/363778
However the author of the stackoverflow answer also mentions that on
Windows you
could probably still use target_link_libraries(binary mylib)!?

3) Or is there another known workaround for this issue.

Thanks,
Kim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180930/6f4a6607/attachment-0001.html>


More information about the CMake mailing list