【问题标题】:CMake project in Visual Studio: How to add additional include and library directories?Visual Studio 中的 CMake 项目:如何添加其他包含和库目录?
【发布时间】:2020-05-25 23:15:07
【问题描述】:

我正在使用 VisualStudio 2019 开发 C++ 代码。

我正在使用 CMake 来配置项目。

我需要使用在我的远程机器上编译的 boost 库。

console application中,我可以将我在Additional Include Directories下进入项目的Properties时需要的包含文件的路径强>领域。 在Additional Include Directories下我可以放boost库的路径。

现在,当我右键单击我的项目以添加我需要的内容时,我找不到 属性

我的boost包含目录/home/ubuntu/boost_1_70_0

我的 boost 库目录/home/ubuntu/boost_1_70_0/stage

如何将它们添加到我的 CMake 项目中?

谢谢!

编辑:

这是我的 CMakelists.txt 文件:

# CMakeList.txt : CMake project for CMakeProject1, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

# Add source to this project's executable.
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")


set(Boost_USE_STATIC_LIBS ON) 
set(Boost_USE_MULTITHREADED OFF)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.70.0 REQUIRED COMPONENTS lambda) 

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS}) 
    add_executable(CMakeProject1 CMakeProject1.cpp) 
    target_link_libraries(CMakeProject1 ${Boost_LIBRARIES})
endif()

# TODO: Add tests and install targets if needed.

这是我的 .cpp 文件:

#include "CMakeProject1.h"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
using namespace std;

int main()
{
    typedef std::istream_iterator<int> in;

    std::cout << "Type in any number: ";

    std::for_each(
        in(std::cin), in(), std::cout
        << (boost::lambda::_1 * 10)
        << "\nType in another number: ");
}

我的boost目录的路径是:/home/ubuntu/boost_1_70_0

我的 boost 库的路径是:/home/ubuntu/boost_1_70_0/stage

当我运行 .cpp 文件时,会出现此 CMake 错误:

CMakeProject1/CMakeLists.txt:13 (find_package) 处的错误 CMake 错误: 找不到“Boost”提供的包配置文件 (要求的版本 1.70.0)具有以下任何名称:

BoostConfig.cmake
boost-config.cmake

将“Boost”的安装前缀添加到CMAKE_PREFIX_PATH或设置
“Boost_DIR”到包含上述文件之一的目录。如果 “Boost”提供了单独的开发包或SDK,请务必 已安装。

【问题讨论】:

标签: c++ visual-studio boost cmake path


【解决方案1】:

CMake 的find_package 命令有两种模式:ModuleConfig 模式。此站点上的许多相关问题都使用 Module 模式提供答案。但是,Boost 1.70 和更高版本提供了一个BoostConfig.cmakeboost-config.cmake 包配置文件,以便与find_package() Config 模式一起使用。 build Boost 时应该会生成包配置文件。例如,如果您将 Boost 1.72 构建到 stage 目录中,则 BoostConfig.cmake 文件位于此处:

boost_1_72_0/stage/lib/cmake/Boost-1.72.0/BoostConfig.cmake

您的错误表明您正在使用 Config 模式,因此您有两种选择:

选项 1

执行错误消息中建议的步骤以帮助 Config 模式包搜索成功完成。通过将此行添加到您的 CMake 文件中 find_package() 之前,将您的 Boost 安装路径附加到前缀路径:

list(APPEND CMAKE_PREFIX_PATH /home/ubuntu/boost_1_70_0)

选项 2

通过在调用find_package()之前设置Boost_NO_BOOST_CMAKE变量来强制模块模式:

set(Boost_NO_BOOST_CMAKE ON)

【讨论】:

  • 请您描述一下find_package命令的两种模式的区别?
  • 我在帖子中添加了指向find_package() 文档的链接,其中包含很多详细信息。 Stack Overflow 上已经有关于这个话题的问题,但是this answer 很好地描述了 ModuleConfig 模式之间的区别。
猜你喜欢
  • 2015-01-03
  • 2011-05-25
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多