问题:
包的使用者无法加载包的二进制文件的共享库。
find_package(MyThirdParty REQUIRED) # MyThirdParty is installed using Conan
find_program(binary_paty MyThirdParty REQUIRED)
execute_process(COMMAND ${binary_path} COMMAND_ERROR_IS_FATAL ANY)
execute_process
命令将失败,因为MyThirdParty
的共享库缺失。
如何打包第三方二进制项目?
最小可复制示例:
第三方项目:
file(WRITE Library.hh "void Func();")
file(WRITE Library.cc "void Func() {}")
add_library(Library SHARED Library.hh Library.cc)
file(WRITE Main.cc "#include "Library.hh"nint main() { Func(); }")
add_executable(MyThirdParty Main.cc)
target_link_libraries(MyThirdParty PRIVATE Library)
install(TARGETS MyThirdParty Library EXPORT MyThirdPartyConfig)
install(EXPORT MyThirdPartyConfig
NAMESPACE MyThirdParty::
DESTINATION lib/cmake/MyThirdParty
)
我尝试用柯南包装第三方:
from conans import ConanFile, CMake, tools
class MyThirdPartyConan(ConanFile):
name = "MyThirdParty"
version = "1.0.0"
settings = "os", "compiler", "build_type", "arch"
def source(self):
tools.download(
filename = "CMakeLists.txt",
url = "https://gist.githubusercontent.com/gccore/9007084e1b307592ae040ceb5745bf5f/raw/419a96712145e8d24d4c9982ab3b7fd31d44b9f0/CMakeLists.txt")
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
消费者:
find_package(MyThirdParty REQUIRED)
find_program(binary_paty MyThirdParty REQUIRED)
execute_process(COMMAND ${binary_path} COMMAND_ERROR_IS_FATAL ANY)
最后conanfile.txt
:
[requires]
MyThirdParty/1.0.0@Ghasem/Test
[generators]
cmake_find_package
但是由于第三方的共享库,消费者使用柯南包的CMake失败。当用户的CMake试图执行MyThirdParty
二进制文件时,它会失败,因为它找不到libLibrary.so
文件。
我的环境:
<子>- 操作系统:
Fedora 35
内核: Linux 5.18.9-100.fc35.x86_64
- 编译器:
GCC 11.3.1 20220421
- CMake:
3.22.2
- 柯南:
1.47.0
在Linux上,我们可以使用patchelf
并在打包状态更改二进制RPATH:
def package(self):
cmake = CMake(self);
cmake.install();
self.run("patchelf --set-rpath '$ORIGIN/../lib' " +
self.package_folder + "/bin/MyThirdParty");
对于Windows来说,只是把共享库文件放在二进制文件旁边,Windows的链接器/加载器并没有努力变得聪明(像Linux的链接器/加载器那样)。