Mingw64: add a statically linked library adds libstdc++ dependency

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

Mingw64: add a statically linked library adds libstdc++ dependency

William Zeitler

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




--

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: Mingw64: add a statically linked library adds libstdc++ dependency

Eric Dönges
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.
--

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: Mingw64: add a statically linked library adds libstdc++ dependency

William Zeitler
What finally worked was:

set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstd++
${CMAKE_CXX_STANDARD_LIBRARIES})

if(MINGW)
     set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
-Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
endif()

Thanks!

On 6/17/19 02:16, Eric Dönges wrote:

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

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