如何使用cmake制作自定义库?

发布于 2025-02-04 14:03:28 字数 1128 浏览 1 评论 0原文

我正在尝试制作一个自定义库,以使用CMAKE安装。这是一个个人公用事业库,因此包含诸如扩展数学库,扩展的向量,文件IO功能等的内容。

我对Cmake非常陌生,并且我正在努力构建/安装它作为库。我希望这是我安装的单个输出文件。我希望能够将其(和所有子库)与

#include <MaxLib.h>

我的makefile添加-lmaxlib一起包含。

我的文件结构就是这样:

- MaxLib.h
 - CMakeLists.txt
 - File
     - File.cpp
     - File.h
     - CMakeLists.txt
 - Geom
     - File.cpp
     - File.h
     - CMakeLists.txt

我尝试编译单个sublibraries ie:

add_library(File SHARED ${CMAKE_CURRENT_SOURCE_DIR}/File.cpp)
target_include_directories(File)

我尝试将它们一起加入:

add_library(${PROJECT_NAME} STATIC
    ${CMAKE_CURRENT_SOURCE_DIR}/MaxLib.h
)

add_subdirectory(File)
add_subdirectory(Geom)
target_link_libraries(MaxLib File)
target_link_libraries(MaxLib Geom)

尽管此编译和我可以安装它,但用于文件的对象文件和GEOM并未与Maxlib

I 链接给人的印象是我做错了这一切...制作图书馆的正确方法是什么?

我应该尝试制作这样的静态图书馆吗?

是否应该在构建单个静态对象的根文件夹中只有一个makefile?

欢迎任何建议!预先感谢

对先前问题的参考

I am trying to make a custom library to install with CMake. This is a personal utilities library so contains things like an extended math library, extended vectors, file IO functions etc.

I am very new to CMake and I am struggling to build/install this as a library. I would like this to be a single output file which I install. And I would like to be able to include it (and all sub libraries) with

#include <MaxLib.h>

and by adding -LMaxLib to my makefile.

My file structure is like this:

- MaxLib.h
 - CMakeLists.txt
 - File
     - File.cpp
     - File.h
     - CMakeLists.txt
 - Geom
     - File.cpp
     - File.h
     - CMakeLists.txt

I have tried compiling the individual sublibraries i.e:

add_library(File SHARED ${CMAKE_CURRENT_SOURCE_DIR}/File.cpp)
target_include_directories(File)

and I have tried joining them all together with:

add_library(${PROJECT_NAME} STATIC
    ${CMAKE_CURRENT_SOURCE_DIR}/MaxLib.h
)

add_subdirectory(File)
add_subdirectory(Geom)
target_link_libraries(MaxLib File)
target_link_libraries(MaxLib Geom)

And although this compiles and I can install it, the object files for File and Geom are not becoming linked with MaxLib

I get the impression that I'm doing this all wrong... What is the correct way to make a library?

Should I be attempting to make a static or shared library like this?

Should it just be one makefile in the root folder which builds a single static object?

Any advice is welcome! Thanks in advance

Reference to previous question

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文