简体   繁体   English

在CLion中使用CMake静态链接Boost库

[英]Link boost libraries statically using CMake in CLion

CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4 CLion 1.2,捆绑了CMake 3.3.2和MinGW-w64 4.8.4

I need to get a single DLL in a result of building that no need any other libraries to work. 由于构建不需要任何其他库,我需要获得一个DLL。 But can't link Boost libraries statically. 但是不能静态链接Boost库。 I bootstrapped and built Boost with corresponding MinGW. 我用相应的MinGW引导并构建了Boost。

    cmake_minimum_required(VERSION 3.3)
    project(SampleProject)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

    set(BOOST_ROOT "..\\lib\\boost_1_59_0")

    set(Boost_USE_STATIC_LIBS ON)
    set(BOOST_COMPONENTS_NEEDED filesystem )

    find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
    if(NOT Boost_FOUND)
        message(FATAL_ERROR "Could not find boost!")
    endif()

    include_directories(${Boost_INCLUDE_DIR})
    set(SOURCE_FILES main.cpp)


    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--kill-at -static-libgcc -static-libstdc++")

    add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}\\..\\..\\output")

    target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})

Build output: O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o): In function error': O:\\SampleProject\\Cpp\\lib\\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference to boost::system::system_category()' 构建输出: O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o):函数error': O:\\SampleProject\\Cpp\\lib\\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference to boost :: system :: system_category()的error': O:\\SampleProject\\Cpp\\lib\\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference to

What else should I do to link with boost? 我还应该怎么做才能与Boost链接?

UPDATE: there is a list of built libraries 更新:有内置库的列表

    libboost_filesystem-mgw48-1_59.a
    libboost_filesystem-mgw48-d-1_59.a
    libboost_filesystem-mgw48-mt-1_59.a
    libboost_filesystem-mgw48-mt-d-1_59.a
    libboost_filesystem-mgw48-mt-s-1_59.a
    libboost_filesystem-mgw48-mt-sd-1_59.a
    libboost_filesystem-mgw48-s-1_59.a
    libboost_filesystem-mgw48-sd-1_59.a
    libboost_system-mgw48-1_59.a
    libboost_system-mgw48-d-1_59.a
    libboost_system-mgw48-mt-1_59.a
    libboost_system-mgw48-mt-d-1_59.a
    libboost_system-mgw48-mt-s-1_59.a
    libboost_system-mgw48-mt-sd-1_59.a
    libboost_system-mgw48-s-1_59.a
    libboost_system-mgw48-sd-1_59.a

This looks like a linker error suggesting that you are not linking to Boost::system You need to add system to BOOST_COMPONENTS_NEEDED . 这看起来像一个链接器错误,提示您未链接到Boost::system需要将system添加到BOOST_COMPONENTS_NEEDED Change this line and see if it helps 更改此行,看看是否有帮助

set(BOOST_COMPONENTS_NEEDED system filesystem )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM