(一)CMake使用
CMake使用
1.注释
# 这是一个CMakeLists.txt文件
cmake_minimum_required(VERSION 3.10)
2.add_executable 定义工程会生成一个可执行程序
add_executable(可执行程序名 源文件名称)
# 样式1:
add_executable(app add.c div.c main.c mult.c sub.c)
3.set
将文件名对应的字符串存储起来
# SET指令的语法是:
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
# 方式1: 各个源文件之间使用空格间隔
set(SRC_LIST add.c div.c main.c mult.c sub.c)
# 方式2: 各个源文件之间使用分号 ; 间隔
set(SRC_LIST add.c;div.c;main.c;mult.c;sub.c)
4.指定C++的标准
# 增加-std=c++11
set(CMAKE_CXX_STANDARD 11)
# 增加-std=c++14
set(CMAKE_CXX_STANDARD 14)
# 增加-std=c++17
set(CMAKE_CXX_STANDARD 17)
5.指定输出的路径
在CMake中指定可执行程序输出的路径,也对应一个宏,叫做EXECUTABLE_OUTPUT_PATH,
它的值还是通过set命令进行设置:
set(HOME /home/heheda/Linux/sort)
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin)
第一行:定义一个变量存储一个绝对路径
第二行:将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
- 如果这个路径中的子目录不存在,会自动生成,无需自己手动创建
6.搜索文件
方式一:在CMake中使用aux_source_directory命令可以查找某个路径下的所有源文件,
命令格式为:
aux_source_directory( )
dir:要搜索的目录
variable:将从dir目录下搜索到的源文件列表存储到该变量中
方式二:
file(GLOB/GLOB_RECURSE 变量名 要搜索的文件路径和文件类型)
GLOB: 将指定目录下搜索到的满足条件的所有文件名生成一个列表,并将其存储到变量中。
GLOB_RECURSE:递归搜索指定目录,将搜索到的满足条件的文件名生成一个列表,并将其存储到变量中。
file(GLOB MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB MAIN_HEAD ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
CMAKE_CURRENT_SOURCE_DIR 宏表示当前访问的 CMakeLists.txt 文件所在的路径。
7.包含头文件
include_directories(${PROJECT_SOURCE_DIR}/include)
PROJECT_SOURCE_DIR宏对应的值就是我们在使用cmake命令时,后面紧跟的目录,一般是工程的根目录。
(二)Calc 项目
heheda@linux:~/Linux/loveDBTeacher-v1$ tree
.
├── CMakeLists.txt
├── include
│ └── head.h
├── main.c
└── src
├── add.c
├── div.c
├── mult.c
└── sub.c
2 directories, 7 files
方式一: 使用aux_source_directory命令
cmake_minimum_required(VERSION 3.10)
project(MyProject)
#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)
# 方式一: 使用aux_source_directory命令
# aux_source_directory(./src SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
add_executable(app main.c ${SRC_LIST})
#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏
#include
#include "head.h"
int main()
{
int a = 20;
int b = 12;
printf("a = %d, b = %d\n", a, b);
printf("a + b = %d\n", add(a, b));
printf("a - b = %d\n", subtract(a, b));
printf("a * b = %d\n", multiply(a, b));
printf("a / b = %f\n", divide(a, b));
return 0;
}
src中的 add.c,div.c,mult.c,sub.c
#include
#include "head.h"
int add(int a, int b)
{
return a+b;
}
#include
#include "head.h"
double divide(int a, int b)
{
return (double)a/b;
}
#include
#include "head.h"
int multiply(int a, int b)
{
return a*b;
}
#include
#include "head.h"
// 你好
int subtract(int a, int b)
{
return a-b;
}
include中的head.h
#ifndef _HEAD_H
#define _HEAD_H
// 加法
int add(int a, int b);
// 减法
int subtract(int a, int b);
// 乘法
int multiply(int a, int b);
// 除法
double divide(int a, int b);
#endif
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5.cd ../bin
6./app
执行结果:
heheda@linux:~/Linux/loveDBTeacher-v1$ mkdir build
heheda@linux:~/Linux/loveDBTeacher-v1$ cd build
heheda@linux:~/Linux/loveDBTeacher-v1/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/loveDBTeacher-v1/build
heheda@linux:~/Linux/loveDBTeacher-v1/build$ make
Scanning dependencies of target app
[ 16%] Building C object CMakeFiles/app.dir/main.c.o
[ 33%] Building C object CMakeFiles/app.dir/src/add.c.o
[ 50%] Building C object CMakeFiles/app.dir/src/div.c.o
[ 66%] Building C object CMakeFiles/app.dir/src/mult.c.o
[ 83%] Building C object CMakeFiles/app.dir/src/sub.c.o
[100%] Linking C executable ../bin/app
[100%] Built target app
heheda@linux:~/Linux/loveDBTeacher-v1/build$ cd ../bin
heheda@linux:~/Linux/loveDBTeacher-v1/bin$ ./app
a = 20, b = 12
a + b = 32
a - b = 8
a * b = 240
a / b = 1.666667
heheda@linux:~/Linux/loveDBTeacher-v1/bin$
注:也可以把方式2的代码换成方式二: 使用GLOB命令 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
cmake_minimum_required(VERSION 3.10)
project(MyProject)
#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
include_directories(${PROJECT_SOURCE_DIR}/include)
# 方式二: 使用GLOB命令 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
# set(SRC ${PROJECT_SOURCE_DIR}/src)
set(SRC ${CMAKE_CURRENT_LIST_DIR}/src)
file(GLOB SRC_LIST ${SRC}/*.c)
add_executable(app main.c ${SRC_LIST})
#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给EXECUTABLE_OUTPUT_PATH宏