c++ - CMake 未将 boost 目录包含在其生成的项目中

标签 c++ visual-studio boost cmake

我一直在尝试创建一个 CMake 项目,然后使用 Visual Studio 2015 对其进行编译。但是,当我生成项目文件时,不包括 boost。这是生成时 CMake 的相关输出:

Boost version: 1.62.0
Found the following Boost libraries:
system
thread
chrono
date_time
atomic
Configuring done
Generating done

而且路径都是正确的。 CMake 应该将包含目录放入 VC++ 目录的什么位置? 构建系统可能哪里出错了?

实际的CMakeLists.txt如下:

#MultiTracker Application
cmake_minimum_required (VERSION 3.1)
project(MultiTracker)

#Additional CMake search modules

#Require C++11
set (CMAKE_CXX_STANDARD 11)
message(STATUS "Generating Makefile for MultiTracker")

file(GLOB SRC_FILES *.cpp)
#Find and link boost
SET(Boost_USE_STATIC_LIBS   ON)
find_package(Boost REQUIRED system thread)

add_executable(MultiTracker ${SRC_FILES})
#Link internal libraries

#Link 3rd party libraries
target_link_libraries(MultiTracker ${Boost_LIBRARIES})

#The native OS thread library
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(MultiTracker Threads::Threads)

main.cpp

//System Includes
#include <iostream>
#include <cstdlib>    

//Library Includes
#include <boost/program_options.hpp>
#include <boost/format.hpp>

//Local Includes    


int main(int iArgc, char *cpArgv[])
{
    std::string confName = "Conf.json", outFileName, inFileName;

    //setup the program options
    boost::program_options::options_description oPODescriptions("Available options");
    oPODescriptions.add_options()
        ("help", "help message")
        ("confFile", boost::program_options::value<std::string>(&confName)->default_value("pclConf.json"), "Name of the configuration file to use");

    boost::program_options::variables_map mapVars;
    try
    {
        boost::program_options::store(boost::program_options::parse_command_line(iArgc, cpArgv, oPODescriptions), mapVars);
        boost::program_options::notify(mapVars);
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << std::endl;
        return 2;
    }

    //print the help message
    if (mapVars.count("help"))
    {
        std::cout << "Stack Overflow Test: " << oPODescriptions << std::endl;
        return ~0;
    }

    std::cout << "Press enter to exit" << std::endl;
    std::cin.get();
}

谢谢!

最佳答案

您还应该在您的问题中包含您如何管理 CMakeLists.txt 文件中的 Boost 依赖项。

如果您使用了 find_package(Boost) ( see reference here ),
你应该将 Boost_INCLUDE_DIRS 添加到你的目标项目包含目录中, 和 Boost_LIBRARIES 到您的项目链接的库。

在此处查看非常相似的问题和答案:How do you add boost libraries in CMakeLists.txt

问题编辑后添加
你错过了:

target_include_directories(MultiTracker PRIVATE ${Boost_INCLUDE_DIRS})

(不确定您的案例中的“PRIVATE”)

关于c++ - CMake 未将 boost 目录包含在其生成的项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005589/

相关文章:

c++ - 使用带有自定义图形的 r_c_shortest 路径( boost )

c++ - 启用用于 boost::lexical_cast 的类

c++ - 在屏幕上定位程序位置

c++ - 大于 GL_MAX_VIEWPORT_DIMS 的视口(viewport)的平铺渲染

c++ - 与 Qt 项目的静态链接

c# - Visual Studio 2014 CTP - 将计算机置于不受支持的状态

c# - 如何在 Visual Studio 2015 中使用 Webbrowser Edge 而不是默认 Web 浏览器

c++ - 如何正确捕获 std 和 boost 异常

c++ - 当前的 x86 架构是否支持非临时加载(来自 "normal"内存)?

c++ - 使用 C++/boost::asio 在网络框架中进行 int/char 转换