• 2. CMake 系列


    1. 编译不使用第三方库的项目

    1.1 项目目录结构

    test/
    ├── build
    ├── CMakeLists.txt
    └── src
        ├── include
        │   └── sub
        │       └── sub.h
        ├── init
        │   └── main.c
        └── sub
            └── sub.c
    

    博主一般写项目都是以这种风格进行划分目录,这个风格也是参考内核风格。

    build: 存放 cmake 生成的相关文件和make 编译生成的相关中间文件

    CMakeLists.txt: 使用cmake 语法编写这个文件,cmake 负责将其转换为相对应makefile

    src: 存放源代码

    include: 存放每个模块头文件,每个模块都有自己的目录;

    1.2 相关代码

    sub.h

    #ifndef _SUB_H
    #define _SUB_H
    
    int sub(const int a, const int b);
    
    #endif
    

    sub.c

    #include "sub/sub.h"
    
    int sub(const int a, const int b)
    {
        return a - b;
    }
    

    main.c

    #include "sub/sub.h"
    
    #include <stdio.h>
    
    
    int main(int argc, char **argv)
    {
        int num = sub(10, 8);
        printf("10 - 8 = %d
    ", num);
    
        return 0;
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    project(project-1)
    
    message("Project Name: " ${PROJECT_NAME})
    
    #设置编译参数
    set(CMAKE_C_FLAGS "-g -Wall")
    
    #设置执行文件输出目录
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
    
    # 添加头文件路径
    include_directories(${PROJECT_SOURCE_DIR}/src/include)
    
    # 递归获取目录下所有的C文件
    file(GLOB_RECURSE c_files ${PROJECT_SOURCE_DIR}/src/*.c)
    
    # 生成执行文件
    add_executable(${PROJECT_NAME} ${c_files})
    
    

    1.3 编译

    进入test目录

    $ cd test
    $ cd build
    $ cmake ..
    $ make
    $ cd ..
    $ tree bin
    

    编译后生成执行文件

    bin
    └── project-1
    
    

    运行秩序文件

    $ cd bin
    $ ./ project-1
    10 - 8 = 2
    

    2. 编译使用第三方库的项目

    2.1 项目目录结构

    test1
    ├── build
    ├── CMakeLists.txt
    └── src
        ├── include
        │   └── sub
        │       └── sub.h
        ├── init
        │   └── main.c
        ├── lib
        │   └── add
        │       ├── include
        │       │   └── add.h
        │       └── lib
        │           └── libadd.a
        └── sub
            └── sub.c
    

    build: 存放 cmake 生成的相关文件和make 编译生成的相关中间文件

    CMakeLists.txt: 使用cmake 语法编写这个文件,cmake 负责将其转换为相对应makefile

    src: 存放源代码

    include: 存放每个模块头文件,每个模块都有自己的目录;

    lib: 存放第三库的头文件和lib文件,若是使用多个第三方库,则需分为不同的目录存放。

    2.2 相关代码

    sub.h

    #ifndef _SUB_H
    #define _SUB_H
    
    int sub(const int a, const int b);
    
    #endif
    

    sub.c

    #include "sub/sub.h"
    
    int sub(const int a, const int b)
    {
        return a - b;
    }
    

    add.h

    #ifndef _ADD_H
    #define _ADD_H
    
    int add(const int a, const int b);
    
    #endif
    

    main.c

    #include "sub/sub.h"
    #include "add.h"
    
    #include <stdio.h>
    
    
    int main(int argc, char **argv)
    {
        int a = 10;
        int b = 8;
    
        printf("%d - %d = %d
    ", a, b, sub(a, b));
        printf("%d + %d = %d
    ", a, b, add(a, b));
    
        return 0;
    }
    
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    project(project-2)
    
    message("Project Name: " ${PROJECT_NAME})
    
    #设置编译参数
    set(CMAKE_C_FLAGS "-g -Wall")
    
    #设置执行文件输出目录
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
    
    # 添加头文件路径
    include_directories(${PROJECT_SOURCE_DIR}/src/include)
    
    # 添加第三方库(add)头文件路径
    include_directories(${PROJECT_SOURCE_DIR}/src/lib/add/include)
    
    #添加库文件搜索路径
    link_directories(${PROJECT_SOURCE_DIR}/src/lib/add/lib)
    
    # 递归获取目录下所有的C文件
    file(GLOB_RECURSE c_files ${PROJECT_SOURCE_DIR}/src/*.c)
    
    # 生成执行文件
    add_executable(${PROJECT_NAME} ${c_files})
    
    # 执行文件链接外部库文件
    target_link_libraries(${PROJECT_NAME} add)
    
    

    2.3 编译

    进入test1目录

    $ cd test1
    $ cd build
    $ cmake ..
    $ make
    $ cd ..
    $ tree bin
    

    编译后生成执行文件

    bin
    └── project-2
    
    

    运行秩序文件

    $ cd bin
    $ ./ project-2
    10 - 8 = 2
    10 + 8 = 18
    
    
  • 相关阅读:
    Tensorflow笔记:反向传播,搭建神经网络的八股,(损失函数loss,均方误差MSE,反向传播训练方法,学习率)
    Tensorflow笔记:神经网络的参数,神经网络的搭建,神经网络的前向传播
    Tensorflow笔记:tensorflow 的基本概念(张量,数据类型,计算图,会话)
    PAT (Advanced Level) Practice 1001 A+B Format
    深度学习Tensorflow 之 MNIST手写数字识别
    MNIST数据集介绍及读取
    深度学习Tensorflow非线性回归案列
    TensorFlow 完整的TensorFlow入门教程
    centos7 解决下载速度慢的问题
    Structs复习 ActionMethod
  • 原文地址:https://www.cnblogs.com/standardzero/p/10781223.html
Copyright © 2020-2023  润新知