【问题标题】:Trying to add Boost libraries to Cmake.txt (Clion Ide)尝试将 Boost 库添加到 Cmake.txt (Clion Ide)
【发布时间】:2016-07-12 17:39:34
【问题描述】:

我看到很多类似但不一样的问题。

我在使用 Clion 在 Cmake 中识别/工作时遇到问题我尝试了多种方法,包括 Clion 附带的 incboost 模板,继续前进。

Cmake 版本 3.4 提升版本 1.60.0

find_package(Boost)
  if (Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIR})
endif()

这就是我上面提到的自动生成的incboost,这不会产生任何结果。

第二次尝试如下

set(Boost_Dir "C:\\Program Files (x86)\\boost_1_60_0\\")
find_package(Boost 1.60.0 COMPONENTS filesystem)
include_directories(${Boost_Dir})

CmakeFiles\Svp.dir/objects.a(main.cpp.obj): 
In function`_static_initialization_and_destruction_0':

C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:221: undefined reference 
to `boost::system::generic_category()'

C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:222: undefined 
reference to `boost::system::generic_category()'

C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:223: undefined
reference to `boost::system::system_category()'

collect2.exe: error: ld returned 1 exit status

mingw32-make.exe[3]: *** [Svp.exe] Error 1

CMakeFiles\Svp.dir\build.make:96: recipe for target 'Svp.exe' failed

CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Svp.dir/all' failed

mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2

CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Svp.dir/rule' failed

mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2

Makefile:117: recipe for target 'Svp' failed

mingw32-make.exe: *** [Svp] Error 2

我的第三次尝试产生了这个

set(boost "C:\\Program Files (x86)\\boost_1_60_0\\boost\\")
INCLUDE_DIRECTORIES(${boost})

 fatal error: boost/filesystem/config.hpp: No such file or directory
 #  include <boost/filesystem/config.hpp>
                                     ^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Svp.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2

最后这里给出的解决方案How do you add boost libraries in CMakeLists.txt 并没有改变我的错误输出。

【问题讨论】:

  • 另外让我澄清一下,我已经在使用不同编译器的各种其他项目中使用了我的 boost 设置,它工作得很好,所以我知道它在 Cmake.txt 中的位置是一个问题
  • 您希望使用哪些编译器?
  • 此时只需 g++
  • 你好像在用mingw,我的回答应该对你有效。

标签: boost cmake


【解决方案1】:

这是我在项目中以可移植方式使用它的方式:

if (WIN32)
    set(BOOST_ROOT "C:/Program Files (x86)/boost_1_60_0/")
    set(Boost_USE_STATIC_LIBS ON)
    set(Boost_USE_MULTITHREAD ON)

    include_directories(${BOOST_ROOT})
    link_directories(${BOOST_ROOT}/stage/lib) # add this before add_executable()
endif()

# Here call your add_executable method
# add_executable(TARGET_NAME ... )

if(NOT MSVC)
    find_package(Boost REQUIRED COMPONENTS date_time filesystem wserialization system serialization thread regex)

    if (Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIRS})
        target_link_libraries(TARGET_NAME ${Boost_LIBRARIES})
    endif()
endif()

有时您需要指定Boost_USE_STATIC_LIBSBoost_USE_MULTITHREAD 变量来帮助find_package 查找boost。

另外请使用set(BOOST_ROOT, $path) 指定正确的boost 库路径。此外,在 find_package 中指定您需要的 boost 包。比如

find_package(Boost REQUIRED COMPONENTS date_time filesystem thread regex)

如果你只需要 date_time filesystem thread regex

让我知道它是否适合你。

【讨论】:

  • 是的,很抱歉我刚刚开始并测试 :) 谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
相关资源
最近更新 更多