我创建了一个简单的conan项目,想在我的c++项目中包含来自conancenter的' net-snmp'包。问题是,当我尝试用cmake构建项目时,我找不到net-snmp
。
为了重现这个错误,下面是我所做的。从模板创建简单的项目:
conan new hello/0.1 --template=cmake_exe
字符串
之后,我将def requirements(self):
包含在新生成的conanfile.py
中:
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
# Optional metadata
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of Hello here>"
topics = ("<Put some tag here>", "<here>", "<and here>")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def requirements(self): # i have only added
self.requires("net-snmp/5.9.1") # these two lines
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["hello"]
型
在CMakeLists.txt
:
find_package(net-snmp 5.9.1 REQUIRED)
型
为了构建它,我使用:用途:
mkdir build && cd build
conan install ..
cmake ..
型
这将导致以下错误:
CMake Error at CMakeLists.txt:4 (find_package):
By not providing "Findnet-snmp.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "net-snmp",
but CMake did not find one.
Could not find a package configuration file provided by "net-snmp"
(requested version 5.9.1) with any of the following names:
net-snmpConfig.cmake
net-snmp-config.cmake
Add the installation prefix of "net-snmp" to CMAKE_PREFIX_PATH or set
"net-snmp_DIR" to a directory containing one of the above files. If
"net-snmp" provides a separate development package or SDK, be sure it has
been installed.
型
我用途:
- Ubuntu 22.04.3
- Conan 1.60.2(由于CI限制,我无法使用2.X)
- CMake 3.27.2
1条答案
按热度按时间ajsxfq5m1#
首先,我看到你正在使用柯南1.x为您的教程。请考虑移动到柯南2.x。有关柯南2.x的更多信息:https://docs.conan.io/2/whatsnew.html
回到你的问题:
你声明了一个新的依赖,但没有告诉Conan生成它的cmake文件。所以你的
conanfile.py
应该看起来或多或少:字符串
现在你需要更新你的
generate
方法:型
CMakeDeps生成器将生成
net-snmp
cmake文件,这些文件用于查找它的头文件、库文件等。但这还不够,你的命令也需要更新,你需要按照命令安装依赖项,然后手动运行cmake。这样做是有代价的,因为你需要告诉cmake工具链文件与你的Conan配置文件相匹配。
因此,当你想构建一个库(基于
conan new
命令和库模板)时,你可以运行:型
conan create命令将导出你的配方,然后在你的柯南缓存中构建它。
然而,如果你只是想直接在你已经存在的C项目中使用一个库,你应该遵循另一种方法,遵循this tutorial。但是为了简化:
仅在C项目的根文件夹中添加新的
conanfile.txt
,内容如下:型
然后,您应该安装您的需求并生成cmake文件:
型
使用
build
将避免将构建缓存文件与源代码混合。另外,您需要将工具链文件传递给cmake,否则,它将无法匹配您的Conan配置文件,也无法找到您安装的依赖项。这个解释是Conan 2.x tutorial的一部分。所以再次,强烈建议你转移到2. x,如果你是第一次尝试柯南。