正确配置CMake中的boost

Properly configuring boost in CMake

本文关键字:中的 boost CMake 配置      更新时间:2023-10-16

我有一个非常简单的boost应用程序:

#include <iostream>
#include "boost/filesystem.hpp"
int main(int /*argc*/, char** /*argv*/) {
    if( boost::filesystem::exists(".") )
        std::cout << "exists" << std::endl;
    return 0;
}

我用CMake配置:

cmake_minimum_required (VERSION 2.8 FATAL_ERROR)
set(USE_BOOST_LIB_HACK TRUE)
#--- Boost (TODO: wrong libraries linked)
find_package (BOOST COMPONENTS filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
if(USE_BOOST_LIB_HACK)
    #--- Now I need to manually append them to avoid missing symbols
    list(APPEND libs /usr/local/lib/libboost_system.dylib)
    list(APPEND libs /usr/local/lib/libboost_filesystem.dylib)
else()
    list(APPEND libs ${Boost_LIBRARIES}) 
    message(STATUS BOOST: ${Boost_LIBRARIES}) #<<< EMPTY :( :(
endif()
#--- Add all sources in this folder
file(GLOB_RECURSE hdrs "*.h")
file(GLOB_RECURSE srcs "*.cpp")
#--- Create executable and link
set(CMAKE_BUILD_TYPE "Debug")
add_executable(boost ${hdrs} ${srcs})
target_link_libraries(boost ${libs})

注意上面文件中的USE_BOOST_LIB_HACK。但是Boost_LIBRARIES不应该有我需要的吗?在上面的配置中,它是一个普通的空字符串。我使用{cmake,boost1.5}从OSX/Homebrew

我的配置是否不正确?

谢谢!

CMake区分大小写。使用find_package(Boost ...),不使用find_package(BOOST ...)。BTW COMPONENTS子选项是可选的,如果REQUIRED使用:

find_package(Boost REQUIRED filesystem system)
  • 可能解释:http://pastebin.com/qQyGnQDL

以下是FindBoost.cmake的文档如何描述宏的正确使用:

#
#   set(Boost_USE_STATIC_LIBS        ON)
#   set(Boost_USE_MULTITHREADED      ON)
#   set(Boost_USE_STATIC_RUNTIME    OFF)
#   find_package( Boost 1.36.0 COMPONENTS date_time filesystem system ... )
#
#   if(Boost_FOUND)
#      include_directories(${Boost_INCLUDE_DIRS})
#      add_executable(foo foo.cc)
#      target_link_libraries(foo ${Boost_LIBRARIES})
#   endif()

这个方法在Linux上确实工作得很好。但我不确定MacOSX/HomeBrew,尽管它没有在宏中特别提到。