CMake编译静态库

新建工程t3,t3的目录结构如下:

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
t3
├── build
├── CMakeLists.txt
└── lib
├── CMakeLists.txt
├── hello.c
└── hello.h

t3/lib下的hello.c和hello.h用来生成库文件。

现在先来编写t3工程目录下的CMakeLists.txt文件:

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(HELLOLIB)
ADD_SUBDIRECTORY(lib)

添加子目录lib

然后编写lib下的CMakeLists.txt文件:

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
SET(LIBHELLO_SRC hello.c)

添加动态库

ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})

添加静态库

ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})

生成动态库的版本号

SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1)

将静态库重新命名为hello

SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")

安装静态库和动态库

INSTALL(TARGETS hello hello_static
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

安装hello.h

INSTALL(FILES hello.h DESTINATION include/hello)

//hello.c

[objc] view plain copy
在CODE上查看代码片派生到我的代码片

include "hello.h"

void HelloFunc()
{
printf("Hello World\n");

}

//hello.h
[objc] view plain copy
在CODE上查看代码片派生到我的代码片

ifndef HELLO_H

define HELLO_H

include <stdio.h>

void HelloFunc();

endif

然后构建:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
cmake -DCMAKE_INSTALL_PREFIX=/tmp ..
make
make install

查看/tmp目录下的lib目录和include目录的目录结构:

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
lib/
├── libhello.a
├── libhello.so -> libhello.so.1
├── libhello.so.1 -> libhello.so.1.2
└── libhello.so.1.2

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
include/
└── hello
└── hello.h

说明所有动态库和头文件都已经安装到了/tmp目录下了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值