• CMake系列(九) CMake 头文件接口库编译及使用




    目录结构

    ├── 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)
    
    • 1
    • 2
    • 3

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

    target_link_libraries(other PUBLIC test_icd)
    
    • 1

    调用关系

    		├──app4.c
    		│	  └──public4.h
    main.c──│	
    		├──public3.h
    		│
    		└── public4.h
    		  └──public3.h
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    源文件

    文件部分只展示重要的部分。
    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");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    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}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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} )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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})
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    编译

  • 相关阅读:
    在行首,行尾添加文本,替换文本中的空格、制表符等
    Selenium-CSS定位
    万字详解全文检索引擎 Elasticsearch
    vue2+TS项目运行和打包错误
    JSP 空教室查询管理系统yeclipse开发mysql数据库bs框架java编程jdbc详细设计
    达梦数据库强制删除schema
    springboot2.x+security+vue2.x后台管理框架---菜单管理(五)
    基于ssm的互联网废品回收/基于web的废品资源利用系统
    深眸科技以需求定制AI视觉解决方案,全面赋能产品外观缺陷检测
    [Azure VM] Azure virtual machine agent status is not ready
  • 原文地址:https://blog.csdn.net/qq_26849933/article/details/126685292