在Visual Studio中的CMake项目:如何添加额外的包含和库目录?

3

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

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

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

控制台应用程序中,当我进入项目的属性下的附加包含目录字段时,我可以放置我需要的包含文件的路径。并且在附加包含目录下,我可以放置boost库的路径。

现在,我右键单击我的项目后找不到属性选项来添加我需要的内容。

boost include directory/home/ubuntu/boost_1_70_0下。

boost libraries directory/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错误:

Error CMake Error at CMakeProject1/CMakeLists.txt:13 (find_package): Could not find a package configuration file provided by "Boost" (requested version 1.70.0) with any of the following names:

BoostConfig.cmake
boost-config.cmake

Add the installation prefix of "Boost" to CMAKE_PREFIX_PATH or set
"Boost_DIR" to a directory containing one of the above files. If "Boost" provides a separate development package or SDK, be sure it has been installed.


1
这回答您的问题吗?如何在CMakeLists.txt中添加Boost库? - Kevin
你可能还想看一下microsoft/vcpkg。 - Mizux
@squareskittles,请问你能看到我的问题编辑吗? - Gonn
@ChrisMM,boostconfig.cmake和boost-config.cmake是我缺少的吗?我需要下载它们吗? - Gonn
我从未使用过boost;当你下载它时,它是否有那些文件?在boost目录中搜索它们。如果它们已经存在,则只需要告诉cmake在哪里找到它们即可。 - ChrisMM
1个回答

0

CMake的find_package命令有两种模式:模块配置模式。许多相关问题在本站上的答案都使用了模块模式。然而,Boost 1.70及更高版本提供了一个BoostConfig.cmakeboost-config.cmake包配置文件,可轻松与find_package() 配置模式一起使用。当您构建Boost时,应该生成包配置文件。例如,如果您将Boost 1.72构建到stage目录中,则BoostConfig.cmake文件位于此处:

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

您的错误提示表明您正在使用Config模式,因此您有两个选项:

选项1

按照错误消息中建议的步骤,帮助Config模式包搜索成功完成。在find_package()之前,在您的CMake文件中添加以下行,将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 命令的两种模式之间的区别? - Gonn
1
我在我的帖子中添加了一个find_package()文档的链接,其中包含许多详细信息。在Stack Overflow上已经有关于这个主题的问题,但是这个答案很好地描述了模块配置模式之间的区别。 - Kevin
请问您能否在这里查看 https://stackoverflow.com/q/60297265/11587528? - Gonn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接