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

cmake 头文件接口库编译-ag真人游戏

阅读 : 91

目录结构

├── app
│ ├── app4
│ │ ├── app4.c
│ │ ├── app4.h
│ │ └── cmakelists.txt
│ └── cmakelists.txt
├── cmakelists.txt
├── main
│ ├── cmakelists.txt
│ └── main.c
└── public
├── cmakelists.txt
├── public3
│ ├── cmakelists.txt
│ └── public3.h
└── public4
├── cmakelists.txt
└── public4.h

结构说明

本章节主要目的是将头文件的代码组织编译成接口库(接口目标)
app目录存放应用层代码
public中存放公共代码
main中存放主函数代码

接口目标简述

根据当前的使用,将头文件编译成接口目标主要有两种应用场景:

  1. 若在开发过程中其他模块并不需要链接当前模块库,但是却需要与当前模块的一些头文件进行共享内存数据交换;
  2. 当前模块只包含头文件,而其它多个模块需要使用当前模块;

cmake接口目标实现方法

cmakelistst.txt编写
下面以代码为例子进行介绍:

add_library(test_icd interface)
target_link_libraries(test_icd interface item1 item2)
target_include_directories(test_icd interface include)

在其它模块就可以通过在该模块的cmakelists.txt文件中添加以下语句来引用上述定义的接口库

target_link_libraries(other public test_icd)

调用关系

		├──app4.c
		│	  └──public4.h
main.c──│	
		├──public3.h
		│
		└── public4.h
		  └──public3.h

源文件

文件部分只展示重要的部分。
app4.c

#include "app4.h"
void app4_print(void)
{
  
    struct public4 s_4;
    s_4.a = 1;
    s_4.b = 2;
    s_4.s_3.a = 3;
    s_4.s_3.b = 4;
    printf("app4: cmake\r\n");
    printf("app4: end\r\n");
}

main.c
#include “app1.h”
#include “app2.h”
#include “app3.h”
#include “public1.h”
#include “public2.h”
#include “public3.h”
#include “app4.h”
#include “static.h”

int main(int argc, char *argv[])
{
printf(“main: cmake\r\n”);
app1_print();
app2_print();
app3_print();
public1_print();
public2_print();
static_print();
app4_print();
printf(“main: end\r\n”);
return 0;
}

头文件

增加public3.h和public4.h头文件
public3.h

#ifndef __public3_h__
#define __public3_h__
#include 
struct public3
{
  
    unsigned char a;
    unsigned char b;
};
#endif

public4.h

#ifndef __public4_h__
#define __public4_h__
#include 
#include "public3.h"
struct public4
{
  
    struct public3 s_3;
    unsigned char a;
    unsigned char b;
};
#endif

cmakelists.txt

cmakelits.txt在系列八的基础上 增加public3 public4 app4,修改main,public层增加public3和public4,app层增加app4

public3 的cmakelists

# set the project name
project (public3)
# add a library with the above sources
add_library(${
  project_name} interface)
add_library(lib::public3 alias ${
  project_name})
target_include_directories( ${
  project_name}
    interface ${
  project_source_dir}
)

public4 的cmakelists

# set the project name
project (public4)
# add a library with the above sources
add_library(${
  project_name} interface)
add_library(lib::public4 alias ${
  project_name})
target_include_directories(${
  project_name} 
    interface ${
  project_source_dir}
)
target_include_directories( ${
  project_name}  interface ${
  public3_source_dir} )

app4 的cmakelists

# set the project name
project (app4)
# add a library with the above sources
add_library(${
  project_name} app4.c)
add_library(lib::app4 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
)
target_link_libraries(${
  project_name}
    lib::public4

main的cmakelists

project(main)
# create the executable
add_executable(${
  project_name} main.c)
target_include_directories( ${
  project_name}
    public ${
  subprojects_source_dir}/include
)
#link_directories( ${
  subprojects_source_dir}/lib )
# link the static library from subproject1 using it's alias sub::lib1
# link the header only library from subproject2 using it's alias sub::lib2
# this will cause the include directories for that target to be added to this project
set(zc_lib
lib::app1
lib::app2
lib::app3
lib::app4
lib::public1
lib::public2
lib::public3
${
  subprojects_source_dir}/lib/libstatic_library.a
)
target_link_libraries(${
  project_name} ${
  zc_lib})

编译

网站地图