简体   繁体   English

C ++项目中的外部库依赖关系

[英]External library dependencies in a c++ project

I am currently trying to setup a project in C++, b that uses the luabind library. 我目前正在尝试使用C + + b建立一个使用luabind库的项目。 Unfortunately on my distro, namely Arch, this library isn't in the official repos and the one in the AUR is out of date and fails to compile. 不幸的是,在我的发行版中,即Arch,该库不在官方存储库中,而AUR中的库已过期且无法编译。

Considering that I need the library only for this project I thought that I could make a sandboxed environment similar to python's virtualenv by building the library then installing(copying) the include files and resulting binaries in 2 sub-directories of my project called include and lib , respectively which I'll add to the linking and include paths when building. 考虑到我只需要该项目的库,我认为我可以通过构建库,然后在项目的2个子目录( includelib )中安装(复制)include文件和生成的二进制文件,来制作类似于python virtualenv的沙盒环境。 ,分别在构建时添加到链接和包含路径。 I understand why distributing the libraries with your project is bad: security and bug fixes in the meantime for example. 我了解为什么随项目一起分发库很不好:例如,同时进行安全性和错误修复。 However distributing DLLs is almost universally done on Windows(which I might do if I cross-compile) and many projects such as games on Linux tend to package their libraries to avoid inconsistencies between disrtos. 但是,分发DLL几乎是在Windows上普遍完成的(如果我进行交叉编译,我可能会这样做),并且许多项目(例如Linux上的游戏)都倾向于打包它们的库,以免出现歧义。 Moreover if ever need a patched or forked version of a lib I doubt I'll ever find it in any official repo. 而且,如果需要lib的补丁或分叉版本,我怀疑我是否会在任何官方仓库中找到它。

So my question is: 所以我的问题是:

  • Is what I described above a common practice? 我上面描述的是一种惯例吗? Should I do it like this? 我应该这样吗?
  • If not, what is the most commonly-agreed-upon solution to this problem? 如果没有,那么最常见的解决方案是什么?

NOTE: I use Cmake for build automation, if it matters 注意:如果重要的话,我使用Cmake进行构建自动化

EDIT: This question slightly overlaps with mine. 编辑: 这个问题与我的重叠。

Your approach is interesting, but it is not necessary for you to devise a working system because it has already been done, and luckily, you are only one step away from the solution ! 您的方法很有趣,但是您不必设计一个工作系统,因为它已经完成了,幸运的是,您距离解决方案仅一步之遥!

Using CMake, it is easy to automate the building and linking of external source code, using the ExternalProject module. 使用CMake,可以使用ExternalProject模块轻松地自动构建和链接外部源代码。

See http://www.kitware.com/media/html/BuildingExternalProjectsWithCMake2.8.html for useful information. 有关有用的信息,请参见http://www.kitware.com/media/html/BuildingExternalProjectsWithCMake2.8.html

This approach has several advantages: 这种方法有几个优点:

  • you do not have to include the library's source code in your repository 您不必在存储库中包含库的源代码
  • you can point to the specific version/git tag of the library that you know works with your software OR the latest release if you are certain it will not break compatibility 您可以指向您知道可以与您的软件一起使用的库的特定版本/ git标记,也可以指向最新版本(如果您确定它不会破坏兼容性)
  • you do not have to write a complete CMakeLists.txt file to build a possibly complex code base 您不必编写完整的CMakeLists.txt文件即可构建可能复杂的代码库
  • you can eventually configure the external project to build as a static library so you will not have to distribute shared libraries 您最终可以配置外部项目以将其构建为静态库,这样就不必分发共享库
  • you can even completely bypass this if not necessary, by trying to detect a working version of the library on your system with the usual find_package call, and only fall back to building it as an external project if not found 您甚至可以通过尝试使用通常的find_package调用检测系统上库的工作版本,从而完全绕过此操作(如果没有必要的话),并且仅在没有找到的情况下才将其构建为外部项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM