How to stop CMake from linking against libstdc++

随声附和 提交于 2021-02-06 08:51:18

问题


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

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

When linking the library, CMake automatically uses -lstdc++. How can I stop it from doing this?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15058403/how-to-stop-cmake-from-linking-against-libstdc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!