如何使用 CMake 直接从目标文件构建库

发布于 2024-12-27 20:17:52 字数 247 浏览 5 评论 0原文

给定一组目标文件,如何使用 CMake 构建静态库?

使用自动工具,我会将

libXXX.a: $(OBJFILES)
    $(AR) cru $@ $(OBJFILES)
    $(RANLIB) $@

OBJFILES 作为目标文件列表。

如何在 CMake 中做到这一点?

编辑:我无法重新编译原始 cxx 文件,我必须使用目标文件(*.o)来创建库。

Given a set of object files, how would you build a static library with CMake ?

With autotools I would do

libXXX.a: $(OBJFILES)
    $(AR) cru $@ $(OBJFILES)
    $(RANLIB) $@

OBJFILES being a list of object files.

How to do that in CMake ?

EDIT: I can't recompile the original cxx files, I have to use the object files (*.o) to create the library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不醒的梦 2025-01-03 20:17:52

添加 自定义命令,例如:

add_custom_command(OUTPUT libXXX.a COMMAND ${AR} cru ${OBJFILES} ).

如果需要,您可以使用 add_custom_targetadd_dependency 将 libXXX.a 添加到特定目标,或者自定义依赖项。

Add a custom command, something like:

add_custom_command(OUTPUT libXXX.a COMMAND ${AR} cru ${OBJFILES} ).

If necessary you can use add_custom_target and add_dependencies to add your libXXX.a to a specific target, or perhaps to customize the dependencies.

冰之心 2025-01-03 20:17:52

CMakeLists.txt中使用add_library

add_library(XXX STATIC foo.c bar.cc baz.cxx)

Use add_library in CMakeLists.txt:

add_library(XXX STATIC foo.c bar.cc baz.cxx)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文