Is there a better way to manage dependency libraries? | 是否有更好的依赖库管理方式? #77
Unanswered
Demonese
asked this question in
Q&A | 提问&回答
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Current Situation
The dependency libraries of LuaSTG Sub are primarily managed in the following ways:
1.1. Manually writing
add_libraryconfigurations (e.g.,imgui)1.2. Or directly using
add_subdirectory(e.g.,luajit)CPM.cmaketo pull the source code of dependencies, then handled in the CMake scripts by:2.1. Letting
CPM.cmakeconfigure the dependency automatically (e.g.,spdlog)2.2. Or downloading only the source code and manually writing
add_libraryconfigurations (e.g.,stb)2.3. Or downloading only the source code, manually writing
add_custom_commandto invoke CMake to build and install the dependency separately, and then manually writingadd_libraryto import it (e.g.,zlib-ng)What Problems Exist?
The most problematic approach is item 2.3 above. Although it can run stably now, the implementation is essentially sh*t.
Why Do We Do This?
Many dependency libraries use wildly inconsistent methods to locate their own transitive dependencies, making it impossible to simply integrate them into our project using
add_subdirectory.The most appropriate way to use such libraries is to build and install them into the system, and then reference them by searching from the installed locations.
For example, when
minizip-ngsearches for its dependencyzlib/zlib-ng, it looks for files likezlib-ng.handzlib.libin standard search paths.Even if you add both libraries to your project like this:
minizip-ngstill cannot find the CMake target ofzlib-ng.Current Workaround
To handle dependencies with complex transitive dependencies, LuaSTG Sub uses
add_custom_commandin its CMake scripts to invoke CMake separately for configuring, building, and installing those libraries, and then manually imports them afterward.Take
freetypeas an example:The process can be summarized as follows:
CMake Configure)CMake Build)CMake Install)add_libraryWhere is a better solution?
Perhaps vcpkg is a better choice.
However, right after getting started,
minizip-nghit me like a ton of bricks: theminizip-ngpackage in vcpkg doesn’t support linking againstzlib-ngat all. Instead, it pulls in and links to a separatezliblibrary. This completely baffled me, so the migration to vcpkg has been paused.现状
LuaSTG Sub 的依赖库主要由以下几种方式管理:
1.1. 手动编写
add_library配置(比如imgui)1.2. 或直接
add_subdirectory(比如luajit)CPM.cmake拉取依赖库源代码,在 CMake 脚本中:2.1. 由
CPM.cmake自动配置(比如spdlog)2.2. 或仅下载源码,手动编写
add_library配置(比如stb)2.3. 或仅下载源码,手动编写
add_custom_command调用 CMake 构建并安装,并手动编写add_library导入(比如zlib-ng)存在哪些问题?
问题最严重的是上述的 2.3 点,虽然目前能稳定地配置依赖库,但实现的方式简直就是屎。
为什么要这么做?
因为很多依赖库自身的依赖库的查找方式千奇百怪,无法简单地通过
add_subdirectory直接添加到我们的项目中。它们最合适的使用方式就是:编译并安装到系统中,引用这些依赖库时则从安装位置搜索。
比如
minizip-ng搜索依赖库zlib/zlib-ng时,会前往搜索路径下查找对应的zlib-ng.h、zlib.lib等文件。即使你通过:
将它们同时添加到项目中,
minizip-ng也无法找到zlib-ng的目标(Target)。目前的解决方式
为了处理这些有着复杂的依赖项的依赖库,LuaSTG Sub 在 CMake 脚本中通过
add_custom_command调用 CMake 单独执行配置、构建、安装,然后再手动导入。以
freetype为例:流程可以概括为:
CMake Configure)CMake Build)CMake Install)add_library更好的方案在哪?
也许 vcpkg 是个更好的选择。
但才刚上手,
minizip-ng就给了我当头一棒,vcpkg 中的minizip-ng居然不支持链接到zlib-ng,而是额外添加zlib库并链接。这个操作给我看傻眼了,所以迁移到 vcpkg 的进展被迫暂停。Beta Was this translation helpful? Give feedback.
All reactions