以下是您的案例的一些答案:
我应该在 package() 方法或 cpp_info.includedirs 中使用 self.copy 吗?有什么区别?
在您的情况下,self.copy 是更好的选择。默认情况下,includedirs 定义为["include"],我们通常仅在软件包安装未按照预期遵循正确的文件夹结构时附加其他文件夹。
这是我的 conanfile.py 基于文档和其他一些东西。
您的示例缺少一个非常重要的属性:exports_sources,没有它,柯南在运行柯南export 命令时不会复制您的头文件。所以,你的食谱应该是这样的:
from conans import ConanFile
class MyLibConan(ConanFile):
name = "MyLib"
version = "1.0"
exports_sources = ["public/*"]
no_copy_sources = True
def package(self):
self.copy("*.hpp", dst="include", src="public")
self.copy("*.h", dst="include", src="public")
您的柯南导出文件将包含列出的文件:
$ conan export . demo/test
Exporting package recipe
MyLib/1.0@demo/test exports_sources: Copied 1 '.h' file: CHeader.h
MyLib/1.0@demo/test exports_sources: Copied 2 '.hpp' files: Someheader.hpp, AnotherHeader.hpp
MyLib/1.0@demo/test: A new conanfile.py version was exported
MyLib/1.0@demo/test: Folder: /home/uilian/.conan/data/MyLib/1.0/demo/test/export
MyLib/1.0@demo/test: Using the exported files summary hash as the recipe revision: f367d0a701a867835b7ac612b90b3d1c
MyLib/1.0@demo/test: Exported revision: f367d0a701a867835b7ac612b90b3d1c
但是你还没有柯南包,你需要运行conan install MyLib/1.0@demo/test --build=MyLib。您可以使用更好的命令来创建包,而不是运行 2 步,conan create:
$ conan create . demo/test
Exporting package recipe
MyLib/1.0@demo/test exports_sources: Copied 1 '.h' file: CHeader.h
MyLib/1.0@demo/test exports_sources: Copied 2 '.hpp' files: Someheader.hpp, AnotherHeader.hpp
MyLib/1.0@demo/test: A new conanfile.py version was exported
MyLib/1.0@demo/test: Folder: /home/conan/.conan/data/MyLib/1.0/demo/test/export
MyLib/1.0@demo/test: Exported revision: f367d0a701a867835b7ac612b90b3d1c
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++
compiler.version=9
os=Linux
os_build=Linux
[options]
[build_requires]
[env]
MyLib/1.0@demo/test: Forced build from source
Installing package: MyLib/1.0@demo/test
Requirements
MyLib/1.0@demo/test from local cache - Cache
Packages
MyLib/1.0@demo/test:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Build
Installing (downloading, building) binaries...
MyLib/1.0@demo/test: Configuring sources in /home/conan/.conan/data/MyLib/1.0/demo/test/source
MyLib/1.0@demo/test: Copying sources to build folder
MyLib/1.0@demo/test: Building your package in /home/conan/.conan/data/MyLib/1.0/demo/test/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
MyLib/1.0@demo/test: Generator txt created conanbuildinfo.txt
MyLib/1.0@demo/test: Calling build()
MyLib/1.0@demo/test: WARN: This conanfile has no build step
MyLib/1.0@demo/test: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' built
MyLib/1.0@demo/test: Build folder /home/conan/.conan/data/MyLib/1.0/demo/test/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
MyLib/1.0@demo/test: Generated conaninfo.txt
MyLib/1.0@demo/test: Generated conanbuildinfo.txt
MyLib/1.0@demo/test: Generating the package
MyLib/1.0@demo/test: Package folder /home/conan/.conan/data/MyLib/1.0/demo/test/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
MyLib/1.0@demo/test: Calling package()
MyLib/1.0@demo/test package(): Packaged 2 '.hpp' files: Someheader.hpp, AnotherHeader.hpp
MyLib/1.0@demo/test package(): Packaged 1 '.h' file: CHeader.h
MyLib/1.0@demo/test: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' created
MyLib/1.0@demo/test: Created package revision d1a54d50f9ded97d33be080f4a1ef606
现在你有更多的输出,因为柯南不仅在导出,而且还在构建和打包你的包。构建部分实际上是一个谎言,因为我们省略了build()。
我收到错误... :(
致命错误:anotherFolder/AnotherHeader.hpp:没有这样的文件或目录
#include
因为这条线:
self.copy("*.hpp", dst="public", src="public", keep_path=False)
你的包裹里会有它:
$ ls -R public/
public/:
AnotherHeader.hpp CHeader.h Someheader.hpp
当您设置keep_path=False 时,您告诉柯南复制所有 *.h 文件并保存到 public/ 文件夹中,但不要保留原始路径 AnotherFolder/
不过,让我们看看新的实现:
self.copy("*.hpp", dst="include", src="public")
self.copy("*.h", dst="include", src="public")
现在保留路径,使用 include/ 作为文件夹。为什么? include/ 是用于头文件的通用名称,就像 src/ 用于 .c 和 .cpp 等源文件一样。尽量保持这样的模式。
$ ls -R include/
include/:
CHeader.h Someheader.hpp anotherFolder
include/anotherFolder:
AnotherHeader.hpp
现在我们可以看到标题文件夹仍然存在,您的错误不会再次发生。
问候。