CMake系列(三) CMake编译出静态库和动态库并使用

CMake系列(三) CMake编译出静态库和动态库并使用



1. 编译静态库

目录结构

├── CMakeLists.txt
├── include
│ └── Static.h
└── src
├── Static.c
└── main.c

源文件

main.c

#include "Static.h"

int main(int argc, char *argv[])
{
    Static_print();
    return 0;
}

Static.c

#include "Static.h"

void Static_print(void)
{
    printf("static hello: cmake\r\n");
}

头文件

Hello.h

#ifndef __STATIC_H__
#define __STATIC_H__
#include <stdio.h>

void Static_print(void);

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(static_library)

############################################################
# Create a library
############################################################

#Generate the static library from the library sources
add_library(static_library STATIC 
    src/Static.c
)

target_include_directories(static_library
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)


############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary 
    src/main.c
)

# link the new static_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
        static_library
)

第三行add_library 创建一个static_library的静态库,源文件为Hello.c
第六行当需要使用static_library静态库创建可执行文件时,需要使用target_link_libraries添加库文件。

编译

	$  mkdir build
	$  cd build/
	$  cmake ..
	$  make

测试

build目录下会出现libstatic_library.a文件

	$  ./hello_cmake
	static hello: cmake

2. 编译动态库

目录结构

├── CMakeLists.txt
├── include
│ └── Hello.h
└── src
├── Hello.c
└── main.c

源文件

main.c

#include "Hello.h"

int main(int argc, char *argv[])
{
    Hello_print();
    return 0;
}

Hello.c

#include "Hello.h"

void Hello_print(void)
{
    printf("shared hello: cmake\r\n");
}

头文件

Hello.h

#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>

void Hello_print(void);

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(hello_library)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(hello_library SHARED 
    src/Hello.c
)
add_library(hello::library ALIAS hello_library)

target_include_directories(hello_library
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(hello_binary
    src/main.c
)

# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
    PRIVATE 
        hello::library
)

第三行add_library 创建一个hello_library的动态库,源文件为Hello.c
第四行add_library 创建一个别名库hello::library,就是给hello_library换个名字,名字叫hello::library
第六行当需要使用hello_library静态库创建可执行文件时,需要使用target_link_libraries添加库文件。

编译

	$  mkdir build
	$  cd build/
	$  cmake ..
	$  make

测试

build目录下会出现libhello_library.so文件
$ ./hello_cmake
static hello: cmake

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖茄子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值