使用CMion中的CMake静态链接增强库

时间:2015-12-11 15:48:51

标签: c++ boost cmake clion

CLION 1.2,捆绑的CMake 3.3.2和MinGW-w64 4.8.4

我需要在构建的结果中获得一个DLL,不需要任何其他库来工作。但是无法静态链接Boost库。我引导并使用相应的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})

构建输出 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()&#中39;

我还应该做些什么来与推特联系?

更新:有一个内置库列表

    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

1 个答案:

答案 0 :(得分:0)

这看起来像链接器错误,表明您没有链接到Boost::system 您需要将system添加到BOOST_COMPONENTS_NEEDED。更改此行并查看是否有帮助

set(BOOST_COMPONENTS_NEEDED system filesystem )