cmake实用指南(二):构建动态库

博客网址:http://blog.chinaunix.net/uid-25696269-id-1435094.html

一、构建动态库

1:目录结构
[onezeroone@ ex-3]$ tree
.
 build
 CMakeLists.txt
lib
CMakeLists.txt
hello.c
hello.h


2 directories, 4 files


2:顶层CMakeLists.txt
[onezeroone@ ex-3]$ cat CMakeLists.txt
PROJECT(EX-3)
ADD_SUBDIRECTORY(lib)
前面我们提到了ADD_SUBDIRECTORT, 如果没有指定第二个参数的话,将和第一个参数内容相同,所以这时候cmake后就会在build目录下自动构建一个lib目录。


3:./lib/CMakeLists.txt
[onezeroone@ lib]$ cat CMakeLists.txt
SET(LIBHELLO_SRC hello.c)
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
ADD_LIBRARY指令语法:
ADD_LIBRARY(libname [SHARED | STATIC | MODULE][EXCLUDE_FROM_ALL] src1 src2 ... srcN)
用于生成库文件
SHARED 共享库 STATIC静态库 MODULE由dyld系统支持
EXCLUDE_FROM_ALL表示这个库不会被默认构建
src× 依赖的源文件


3:构建共享库
[onezeroone@ build]$ cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c 
-- Check for working CXX compiler: /usr/bin/c -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Warning (dev) in CMakeLists.txt:

No cmake_minimum_required command is present. A line of code such as

cmake_minimum_required(VERSION 2.8)


should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/onezeroone/work/backup/cmake/ex-3/build
[onezeroone@ build]$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake lib Makefile
[onezeroone@ build]$ make
Scanning dependencies of target hello
[100%] Building C object lib/CMakeFiles/hello.dir/hello.o
Linking C shared library libhello.so
[100%] Built target hello
[onezeroone@ build]$ cd lib/
[onezeroone@ lib]$ ls
CMakeFiles cmake_install.cmake libhello.so Makefile
[onezeroone@ lib]$
现在我们可以看到so文件了。


4:为共享库添加版本号 
需要在./lib/CMakeLists.txt最后中添加:
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)
VERSION指代动态库版本 SOVERSION指代API版本
然后我们再重新构建
[onezeroone@ lib]$ pwd
/home/onezeroone/work/backup/cmake/ex-3/build/lib
[onezeroone@ lib]$ ll
total 32
drwxr-xr-x 3 onezeroone onezeroone 4096 2011-05-05 00:41 ./
drwxr-xr-x 4 onezeroone onezeroone 4096 2011-05-05 00:41 ../
drwxr-xr-x 3 onezeroone onezeroone 4096 2011-05-05 00:41 CMakeFiles/
-rw-r--r-- 1 onezeroone onezeroone 1167 2011-05-05 00:19 cmake_install.cmake
lrwxrwxrwx 1 onezeroone onezeroone 13 2011-05-05 00:41 libhello.so -> libhello.so.1*
lrwxrwxrwx 1 onezeroone onezeroone 15 2011-05-05 00:41 libhello.so.1 -> libhello.so.1.0*
-rwxr-xr-x 1 onezeroone onezeroone 6686 2011-05-05 00:41 libhello.so.1.0*
-rw-r--r-- 1 onezeroone onezeroone 5175 2011-05-05 00:41 Makefile
我们可以看到so后面的数字了吧,那就是我们的版本了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值