gpt4 book ai didi

c++ - Visual Studio 中的 CMake 项目 : How to add additional include and library directories?

转载 作者:行者123 更新时间:2023-12-05 07:13:33 29 4
gpt4 key购买 nike

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

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

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

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

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

我的boost include目录/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 错误:

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.

最佳答案

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

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

选项 1

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

list(APPEND CMAKE_PREFIX_PATH /home/ubuntu/boost_1_70_0)

选项 2

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

set(Boost_NO_BOOST_CMAKE ON)

关于c++ - Visual Studio 中的 CMake 项目 : How to add additional include and library directories?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60153745/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com