gpt4 book ai didi

c++ - CMake 使用错误的链接器标志(动态与静态)

转载 作者:搜寻专家 更新时间:2023-10-31 02:24:06 25 4
gpt4 key购买 nike

我正在静态编译我的程序,如下所示:

cmake_minimum_required(VERSION 2.8)

project(statictest)

set(LINK_SEARCH_START_STATIC ON)
set(LINK_SEARCH_END_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")

find_package(Boost REQUIRED COMPONENTS filesystem system program_options regex thread chrono)

include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${Boost_LIBRARIES})

add_executable(test
"src/test/main.cpp"
)
target_link_libraries(test ${LIBS})

链接器行应该包含 BOOST 的静态链接,因为我已经找到了库的 static 版本。

这是 cmake 生成的链接器行:

/usr/bin/c++   -g    -static -static-libgcc -static-libstdc++ CMakeFiles/test.dir/src/test/main.cpp.o -o test -rdynamic -Wl,-Bdynamic /usr/local/lib/libboost_filesystem.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_program_options.a /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_thread.a /usr/local/lib/libboost_chrono.a

请注意所有以.a 结尾的static BOOST 库之前的-Wl,-bdynamic。我如何强制 CMAKE 将这些与静态标志链接起来?

最佳答案

LINK_SEARCH_END_STATIC是目标属性,您必须将其应用于目标本身:

SET_TARGET_PROPERTIES(test PROPERTIES LINK_SEARCH_END_STATIC 1)

关于c++ - CMake 使用错误的链接器标志(动态与静态),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28438442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com