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

William Zeitler william at williamzeitler.com
Sat Jun 15 15:33:38 EDT 2019


I want to accomplish a c++ executable that links to another c++ library 
of my own without a libstdc++ dependency. I can create such a c++ 
executable without the library without difficulty. But as soon as I link 
it to my library, the executable now has a libstdc++ dependency. A 
requirement is to NOT need to copy libstdc++6.dll (or any other .dll) 
onto Windows.

I'm building on Linux (Centos 7) using the latest mingw64 
(x86_64-w64-mingw32-gcc (GCC) 4.9.3), cross compiling to Windows. 
Building on MSYS2/MingW has the same unwanted result.

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.)

The example has this structure (contents below):

* hello_lib/library.cpp
* hello_lib/library.h
* hello_lib/CMakeLists.txt
* hello_c/main.cpp
* hello_c/CMakeLists.xt
* buildme.sh
* toolchain.cmake

###### BEGIN CODE ######
## hello_lib/library.h:
int hello();

## hello_lib/library.cpp:
#include "library.h"
int hello() {
     return 666;
}

## hello_lib CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(hello_lib)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -static-libgcc 
-static-libstdc++")
add_library(hello_lib STATIC library.cpp)

## hello_c/main.cpp:
#include <iostream>
#include "../hello_lib/library.h"
int main() {
     std::cout << "Hello, world" << std::endl;
     //COMMENT ME OUT:
     printf("x = %d\n", hello());
     return 0;
}

## hello_c CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(hello_c)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -static-libgcc 
-static-libstdc++")
link_directories(../hello_lib/cmake-build-debug)
add_executable(hello_c main.cpp)
# COMMENT ME OUT:
target_link_libraries(hello_c -static-libgcc -static-libstdc++ 
libhello_lib.a)

## buildme.sh:
#!/bin/bash
set -e
for i in hello_lib hello_c
do
     rm -rf $i/cmake-build-debug
     mkdir $i/cmake-build-debug
     pushd $i/cmake-build-debug
     # For building Win64 on Linux (with MinW64):
     cmake .. -DCMAKE_TOOLCHAIN_FILE="../toolchain.cmake" 
-DCMAKE_BUILD_TYPE=Debug
     # For building Win64 on Win64/MSYS2/MinW64:
     #cmake .. -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Debug
     # For building Linux on Linux
     #cmake .. -DCMAKE_BUILD_TYPE=Debug
     make
     popd
done

## toolchain.cmake:
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

###### END CODE ######

With both MingW64 on Linux, and MingW64/MSYS2 on Windows 10, I get the 
libstdc++ dependency when I link with the 'hello_lib' library. In both 
cases if I comment out the reference to hello_lib, the libstdc++ 
dependency is eliminated.

I would be very grateful for your insight!

William Zeitler



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190615/a1495959/attachment.html>


More information about the CMake mailing list