How to stop CMake from linking against libstdc++

前端 未结 2 1930
余生分开走
余生分开走 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:06
    set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
    set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
    set_target_properties(yourtarget PROPERTIES LINKER_LANGUAGE C)
    

    Source: http://cmake.3232098.n2.nabble.com/setting-LINKER-LANGUAGE-still-adds-lstdc-td7581940.html

    0 讨论(0)
  • 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'.

    0 讨论(0)
提交回复
热议问题