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\n", 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\n", a, b, sub(a, b));
    printf("%d + %d = %d\n", 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
出处:https://www.cnblogs.com/standardzero/p/10781223.html
posted @ 2020-01-14 21:22  jingsupo  阅读(1833)  评论(0编辑  收藏  举报

欢迎光临