cmake中经常使用find_package寻找模块,使用起来非常方便.find_package的原理是什么呢?如何自己写个模块提供给别人使用?如果别人希望用find_package的形式使用你的模块中的库,我该如何用cmake写这个库呢?
下面用一个例子来说明:
myapp程序通过find_package调用mylib中的库
1.myapp工程
myapp工程目录如下
.
├── CMakeLists.txt
└── myapp.c
其中CMakeLists.txt
project(myapp)
cmake_minimum_required(VERSION 2.8)
SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/home/rootroot/newworkspace/mylib/install")
find_package(mylib REQUIRED)
add_executable(${PROJECT_NAME} myapp.c)
target_link_libraries(${PROJECT_NAME} mylib::mylib)
通过find_package来寻找我的自定义模块mylib,SET(CMAKE_PREFIX_PATH)告诉cmake在我指定的位置寻找模块,我的mylib安装在/home/rootroot/newworkspace/mylib/install路径下.
myapp.c代码如下
#include<stdio.h>
#include <mylib.h>
int main()
{
hello();
return 0;
}
2.mylib工程
最后生成的mylib工程结构如下
.
├── CMakeLists.txt
├── include
│ └── mylib.h
├── install
│ ├── include
│ │ └── mylib.h
│ └── lib
│ ├── cmake
│ │ └── mylib
│ │ ├── mylib-config.cmake
│ │ └── mylib-config-noconfig.cmake
│ ├── libmylib.a
│
└── mylib.c
其中install是要myapp需要调用的库文件夹.mylib-config.cmake是由cmake自动生成的文件.findpack需要找的.cmake文件才能寻找包成功.
mylib.c代码如下
#include<stdio.h>
#include"mylib.h"
void hello()
{
printf("hello");
}
mylib.h
#ifndef MYLIB_H
#define MYLIB_H
#include <stdio.h>
void hello();
#endif
关键是如何写CMakeLists.txt,要能生成find_package能够找到的模块(包).
CMakeLists.txt代码如下:
cmake_minimum_required(VERSION 2.8)
add_library(mylib mylib.c include/mylib.h)
SET(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)
target_include_directories(mylib PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER "include/mylib.h")
install(TARGETS mylib
EXPORT mylib-targets
PUBLIC_HEADER DESTINATION include
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
install(EXPORT mylib-targets
NAMESPACE mylib::
FILE mylib-config.cmake
DESTINATION lib/cmake/mylib)
分句解析:
add_library(mylib mylib.c include/mylib.h)
表示将mylib.c编译成mylib链接库文件
SET(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)
表示将编译好的库文件安装在当前工程的install文件夹下.
target_include_directories(mylib PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
BUILD_INTERFACE表示源文件夹在哪
INSTALL_INTERFACE表示安装文件夹在哪
set_target_properties(mylib PROPERTIES PUBLIC_HEADER "include/mylib.h")
表示安装的头文件名称是mylib.h.
install(TARGETS mylib
EXPORT mylib-targets
PUBLIC_HEADER DESTINATION include
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
这是安装的一些属性:注意EXPORT选项标识了库文件为供别的程序使用的,LIBRARY DESTINATION 表示库文件的目标文件夹lib 的路径为相对于CMAKE_INSTALL_PREFIX的路径.
其中
install(EXPORT mylib-targets
NAMESPACE mylib::
FILE mylib-config.cmake
DESTINATION lib/cmake/mylib)
安装的时候还需要生成 mylib-config.cmake文件才能让find_package找到.
注意.cmake文件必须是命名如mylib-config.cmake或者
MylibConfig.cmake才能找到.
3编译运行
先编译安装mylib
在mylib文件夹下
mkdir build
cd build
cmake ..
make
make install
在myapp文件夹下
mkdir build
cd build
cmake ..
make
./myapp
显示
hello
最后,为了方便大家理解,将代码打包上传,欢迎大家下载
https://download.csdn.net/download/ktigerhero3/10773044
参考文献
https://stackoverflow.com/questions/31537602/how-to-use-cmake-to-find-and-link-to-a-library-using-install-export-and-find-pac
https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#package-configuration-file
cmake的find_package指令是如何运行的