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

CMake 头文件接口库编译

阅读 : 1350

目录结构

├── 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 <stdio.h>

struct public3
{
  
    unsigned char a;
    unsigned char b;
};

#endif

public4.h

#ifndef __PUBLIC4_H__
#define __PUBLIC4_H__
#include <stdio.h>
#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})

编译