菜鸟笔记
提升您的技术认知

cmake 静态库和动态库-ag真人游戏

阅读 : 110

目录结构

├── 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 
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

目录结构

├── 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 
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

网站地图