如何使用 cmake 创建共享库?
问题描述
我已经写了一个库,我曾经使用自己编写的 Makefile 进行编译,但现在我想切换到 cmake.树看起来像这样(我删除了所有不相关的文件):<预><代码>.├── 包括│ ├── animation.h│ ├── buffers.h│ ├── ...│ ├── vertex.h│ └── world.h└── src├──动画.cpp├──缓冲区.cpp├── ...├── 顶点.cpp└── world.cpp
所以我想要做的只是将源代码编译成共享库,然后与头文件一起安装.
我发现的大多数示例都使用一些共享库编译可执行文件,但绝不仅仅是一个普通的共享库.如果有人能告诉我一个使用 cmake 的非常简单的库也会很有帮助,所以我可以以此为例.
始终指定 cmake
您应该声明一个项目.cmake
说这是强制性的,它将定义方便的变量 PROJECT_NAME
、PROJECT_VERSION
和 PROJECT_DESCRIPTION
(后一个变量需要 cmake3.9):
声明一个新的库目标.请避免使用file(GLOB ...)
.此功能不提供编译过程的有人参与的掌握.如果你懒惰,复制粘贴 ls -1 sources/*.cpp
的输出:
设置 VERSION
属性(可选,但这是一个好习惯):
您还可以将SOVERSION
设置为VERSION
的主要编号.所以 libmylib.so.1
将是 libmylib.so.1.0.0
的符号链接.
声明您的库的公共 API.将为第三方应用程序安装此 API.将它隔离在您的项目树中是一个很好的做法(比如将它放在 include/
目录中).请注意,不应安装私有头文件,我强烈建议将它们与源文件放在一起.
如果您使用子目录,则包含诸如 ../include/mylib.h"
之类的相对路径不是很方便.因此,在包含的目录中传递一个顶级目录:
或
为您的库创建安装规则.我建议使用 GNUInstallDirs
中定义的变量 CMAKE_INSTALL_*DIR
:
并声明要安装的文件:
您也可以导出 pkg-config
文件.此文件允许第三方应用程序轻松导入您的库:
- 使用 Makefile,参见
pkg-config
- 对于 Autotools,请参阅
PKG_CHECK_MODULES
- 使用 cmake,请参阅
pkg_check_modules
创建一个名为 mylib.pc.in
的模板文件(参见 pc(5) 手册页了解更多信息):
在你的 CMakeLists.txt
中,添加一个规则来扩展 @
宏(@ONLY
要求 cmake 不扩展表单的变量${VAR}
):
最后,安装生成的文件:
您也可以使用 cmake <代码>导出功能.不过这个功能只兼容cmake
,我觉得很难用.
最后整个 CMakeLists.txt
应该看起来像:
编辑
正如评论中提到的,为了符合标准,您应该能够生成静态库和共享库.这个过程有点复杂,与最初的问题不符.但值得一提的是,它在这里有很好的解释.
I have written a library that I used to compile using a self-written Makefile, but now I want to switch to cmake. The tree looks like this (I removed all the irrelevant files):
So what I am trying to do is just to compile the source into a shared library and then install it with the header files.
Most examples that I have found compile executables with some shared libraries but never just a plain shared library. It would also be helpful if someone could just tell me a very simple library that uses cmake, so I can use this as an example.
Always specify the minimum required version of cmake
You should declare a project. cmake
says it is mandatory and it will define convenient variables PROJECT_NAME
, PROJECT_VERSION
and PROJECT_DESCRIPTION
(this latter variable necessitate cmake 3.9):
Declare a new library target. Please avoid the use of file(GLOB ...)
. This feature does not provide attended mastery of the compilation process. If you are lazy, copy-paste output of ls -1 sources/*.cpp
:
Set VERSION
property (optional but it is a good practice):
You can also set SOVERSION
to a major number of VERSION
. So libmylib.so.1
will be a symlink to libmylib.so.1.0.0
.
Declare public API of your library. This API will be installed for the third-party application. It is a good practice to isolate it in your project tree (like placing it include/
directory). Notice that, private headers should not be installed and I strongly suggest to place them with the source files.
If you work with subdirectories, it is not very convenient to include relative paths like "../include/mylib.h"
. So, pass a top directory in included directories:
or
Create an install rule for your library. I suggest to use variables CMAKE_INSTALL_*DIR
defined in GNUInstallDirs
:
And declare files to install:
You may also export a pkg-config
file. This file allows a third-party application to easily import your library:
- with Makefile, see
pkg-config
- with Autotools, see
PKG_CHECK_MODULES
- with cmake, see
pkg_check_modules
Create a template file named mylib.pc.in
(see pc(5) manpage for more information):
In your CMakeLists.txt
, add a rule to expand @
macros (@ONLY
ask to cmake to not expand variables of the form ${VAR}
):
And finally, install generated file:
You may also use cmake EXPORT
feature. However, this feature is only compatible with cmake
and I find it difficult to use.
Finally the entire CMakeLists.txt
should looks like:
EDIT
As mentioned in comments, to comply with standards you should be able to generate a static library as well as a shared library. The process is bit more complex and does not match with the initial question. But it worths to mention that it is greatly explained here.
这篇关于如何使用 cmake 创建共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!