[CMake] Mingw64: add a statically linked library adds libstdc++ dependency

Eric Dönges doenges at mvtec.com
Mon Jun 17 05:16:48 EDT 2019


On 15.06.19 21:33, William Zeitler wrote:
> In the example below, two lines are marked "COMMENT ME OUT": one in
> hello_c/main.cpp and the other in hello_c/CMakeLists.txt. If you comment
> these out, the reference to the hello_lib library is removed; the
> project builds and the executable executes on Windows 10 without a
> libstdc++ dependency. If you uncomment the two lines, the function in
> the hello_lib library is linked in; the project builds, but won't
> execute on Windows 10 due to the libstdc++ dependency. (Note: in
> powershell it silently fails, in an old-school dos/cmd box it displays
> an error message.)

I think your problem is that CMAKE_CXX_FLAGS are only used when
compiling, not linking, so your hello_lib is linked without the "-static
-static-libgcc -static-libstdc++" options and thus links against the
shared libstdc++. You could try either of the following:

1) Add a "target_link_libraries(hello_lib -static-libgcc
-static-libstdc++)".

2) Add "string(APPEND CMAKE_SHARED_LINKER_FLAGS "-static-libgcc
-static-libstdc++)" to your project. Note that this will probably need
to be done before defining any of your (library) targets. Also note that
this will link any shared library in your project with the static
libstdc++, which may or may not be what you want.

Disclaimer - I haven't tried if any of this actually solves your
problem; I just think it should.


More information about the CMake mailing list