【问题标题】:Configuring Boost Library in CLion在 CLion 中配置 Boost 库
【发布时间】:2019-12-04 13:18:35
【问题描述】:

这是我第一次使用 Boost lib,所以我当然遇到了问题:

首先这是我的项目的 CMAKELISTS.txt:

    ===========================================
    src/CMAKELISTS.txt:
    =========================================== 
    cmake_minimum_required(VERSION 3.15)
    project(My_String)

    set(CMAKE_CXX_STANDARD 17)
    set(SOURCE_FILES MyString.cpp MyString.h main.cpp)

    add_executable(My_String_src ${SOURCE_FILES})
    ===========================================
    test/CMAKELISTS.txt:
    ===========================================
    cmake_minimum_required(VERSION 3.15)
    project(My_String)

    set(CMAKE_CXX_STANDARD 17)
    set(Boost_USE_STATIC_LIBS OFF)
    set(SOURCE_FILES MyStringTest.cpp)
    set(Boost_INCLUDE_DIR "C:\\Program Files\\Boost\\boost_1_71_0")
    set(Boost_LIBRARIES "C:\\Program Files\\Boost\\boost_1_71_0")

    find_package (Boost COMPONENTS unit_test_framework)

    include_directories(${Boost_INCLUDE_DIR})
    include_directories(../src)

    add_executable (Boost_Tests_run ${SOURCE_FILES})

    target_link_libraries (Boost_Tests_run ${Boost_LIBRARIES})
    ==============================================
    top_level CMAKELISTS.txt for the whole project:
    ==============================================
    cmake_minimum_required(VERSION 3.15)
    project(My_String)

    set(CMAKE_CXX_STANDARD 17)

    add_subdirectory(src)
    add_subdirectory(test)

注意:我已经构建了 BOOST 库!enter code here 在 MyStringTest.cpp 我有:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE MyString_Test_Suite

#include <iostream>
#include "MyString.h"
#include "MyString.cpp"
#include <boost/test/unit_test.hpp>

我在构建项目时遇到的错误:

    [ 50%] Building CXX object test/CMakeFiles/Boost_Tests_run.dir/MyStringTest.cpp.obj
    [100%] Linking CXX executable Boost_Tests_run.exe
    C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\ar.exe: unable to rename 'CMakeFiles\Boost_Tests_run.dir/objects.a'; reason: File exists
    mingw32-make.exe[3]: *** [test\CMakeFiles\Boost_Tests_run.dir\build.make:86: test/Boost_Tests_run.exe] Error 1
    mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:141: test/CMakeFiles/Boost_Tests_run.dir/all] Error 2
    mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:148: test/CMakeFiles/Boost_Tests_run.dir/rule] Error 2
    mingw32-make.exe: *** [Makefile:130: Boost_Tests_run] Error 2

我的配置有什么问题? 我需要做什么来修复它?

【问题讨论】:

标签: c++ boost cmake clion lib


【解决方案1】:

代码告诉链接器将此库../src 链接到您的可执行文件。这是一个无效的操作,所以删除这一行:

target_link_libraries(Boost_Tests_run ../src)

target_link_libraries() 的参数保留用于目标库名称(包括库文件的完整路径)。 ../src 参数不符合其中任何一个标准,因为它只是一个目录。


编辑:另一个潜在问题是您的 Boost_LIBRARIES 变量。同样,如果 target_link_libraries() 仅包含目录路径,则不应将其提供给它。它应该包含库的完整路径,包括库名称。

但是,现代的FindBoost.cmake 模块提供了更好的方法。如果您知道要使用的 Boost 组件(例如 filesystem),则应在 find_package() 调用中指定这些组件:

find_package(Boost REQUIRED COMPONENTS filesystem)

然后,find 模块将为您填充 imported 目标,您可以像这样链接到它们:

target_link_libraries(Boost_Tests_run PUBLIC Boost::filesystem)

我建议看一下page 上的一些示例。

【讨论】:

  • 删除建议删除的行后,在构建项目时出现此错误:[ 60%] Built target My_String_src [ 80%] Linking CXX executable Boost_Tests_run.exe C:\PROGRA~1 \MINGW-~1\X86_64~1.0-P\mingw64\bin\ar.exe:无法重命名“CMakeFiles\Boost_Tests_run.dir/objects.a”;原因:文件存在 mingw32-make.exe[2]: *** [test\CMakeFiles\Boost_Tests_run.dir\build.make:86: test/Boost_Tests_run.exe] 错误1 mingw32-make.exe[1]: ** * [CMakeFiles\Makefile2:141: test/CMakeFiles/Boost_Tests_run.dir/all] 错误 2 mingw32-make.exe: *** [Makefile:83: all] 错误 2
  • @AmjadAlissa 我扩展了我的答案。就像我发布的第一个答案一样,您不能简单地在调用target_link_libraries() 时使用目录,它们必须是实际的库。我希望这会有所帮助。
  • 非常感谢您的回答!这是我第一次使用 boost,我真的花了很多时间试图弄清楚如何使项目工作,但到目前为止没有成功。我再次按照您所说的做了,我将 cmakelists.txt 修改为: find_package(Boost REQUIRED COMPONENTS unit_test_framework) target_link_libraries (Boost_Tests_run Boost::unit_test_framework) 我收到此错误:找不到 Boost (missing: unit_test_framework) (found version "1.71.0")
猜你喜欢
  • 2016-07-30
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2020-06-17
  • 2016-07-12
相关资源
最近更新 更多