CMake编译静态库

mac2022-06-30  73

新建工程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目录下了。

转载于:https://www.cnblogs.com/Dennis-mi/articles/6646637.html

相关资源:cmake编译静态库/动态库/可执行文件demo
最新回复(0)