How to stop CMake from linking against libstdc++

前端 未结 2 1942
余生分开走
余生分开走 2021-02-05 19:37

I have a very simple CMakeLists.txt for a C++ project, which builds a shared library:

add_library(foo SHARED
    ${HDR_PUBLIC}
    ${SOURCES})

2条回答
  •  时光取名叫无心
    2021-02-05 20:15

    You can add -stdlib=libc++ to compiler flags.

    Simple example:

    cmake_minimum_required(VERSION 2.8.4)
    project(test)
    set(CMAKE_VERBOSE_MAKEFILE TRUE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")
    add_executable(test main.cpp)
    

    Give output:

    "/usr/bin/ld" ... -o test ... -lc++ ...
    

    By default:

    cmake_minimum_required(VERSION 2.8.4)
    project(test)
    set(CMAKE_VERBOSE_MAKEFILE TRUE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v")
    add_executable(test main.cpp)
    

    Link to stdc++:

    "/usr/bin/ld" ... -o test ... -lstdc++ ...
    

    [update]

    If you don't need to link to c++ lib at all - use '-nodefaultlibs' as linker flag and '-nostdinc++' to compiler flag. You may need to link some default libraries, like '-lSystem'.

提交回复
热议问题