│ CMakeLists.txt
│ main.c
│
├─add
│ add.c
│ add.h
│
├─multiply
│ multiply.c
│ multiply.h
│
└─sub
sub.c
sub.h
CMakeLists.txt
参考1
指定每个目录后再手动查找该目录下的C文件
# 指定所须的cmake最小版本号
cmake_minimum_required(VERSION 3.0.0)
# 指定C编译器gcc安装绝对路径
set(CMAKE_C_COMPILER E:/MSYS64/mingw64/bin/gcc.exe)
# 指定C++编译器g++安装绝对路径
set(CMAKE_CXX_COMPILER E:/MSYS64/mingw64/bin/g++.exe)
# 指定输出目标名称 app
project(app)
# 添加源文件main.c
set(allsrcs main.c)
# 查询各个子目录的c文件
aux_source_directory(add srcs)
set(allsrcs ${allsrcs} ${srcs})
aux_source_directory(sub srcs)
set(allsrcs ${allsrcs} ${srcs})
aux_source_directory(multiply srcs)
set(allsrcs ${allsrcs} ${srcs})
# 指生成可执行类型的目标
add_executable(app ${allsrcs})
参考2
只须配置每个目录,之后自动查找所有目录下的C文件
# 指定所须的cmake最小版本号
cmake_minimum_required(VERSION 3.0.0)
# 指定C编译器gcc安装绝对路径
set(CMAKE_C_COMPILER E:/MSYS64/mingw64/bin/gcc.exe)
# 指定C++编译器g++安装绝对路径
set(CMAKE_CXX_COMPILER E:/MSYS64/mingw64/bin/g++.exe)
# 指定输出目标名称 app
project(app)
# 添多个源文件目录,为了阅读和扩展方便,一行一个目录
set(src_dirs ./)
set(src_dirs ${src_dirs} add)
set(src_dirs ${src_dirs} multiply)
set(src_dirs ${src_dirs} sub)
# 指定头文件include目录
include_directories(${src_dirs})
# 定义源文件列表变量,初值不能空,一般为main.c
set(allsrcs "main.c")
# 遍历所有目录中的源文件,存于变量 allsrcs
foreach(onedir ${src_dirs})
aux_source_directory(${onedir} allsrcs)
endforeach()
# 指生成可执行类型的目标
add_executable(app ${allsrcs})
示例的C文件和h文件
main.c
#include "stdlib.h"
#include "stdio.h"
#include "add/add.h"
#include "sub/sub.h"
#include "multiply/multiply.h"
int main(void) {
printf("Hello, CMake buile on windows MSYS!rn");
printf("This is an C project builded from CMake toolrn");
add(500,-6987);
sub(9876,90256);
multiply(457,5487);
return 0;
}
add.c
#include "stdlib.h"
#include "stdio.h"
int add(int arg1, int arg2)
{
int returnData = arg1 + arg2;
printf("%s(%d,%d)=%drn", __func__, arg1, arg2,returnData);
return returnData;
}
sub.c
#include "stdlib.h"
#include "stdio.h"
int sub(int arg1, int arg2)
{
int returnData = arg1 - arg2;
printf("%s(%d,%d)=%drn", __func__, arg1, arg2,returnData);
return returnData;
}
multiply.c
#include "stdlib.h"
#include "stdio.h"
int multiply(int arg1, int arg2)
{
int returnData = arg1 * arg2;
printf("%s(%d,%d)=%drn", __func__, arg1, arg2,returnData);
return returnData;
}
构建项目与编译源码
cmake生成Makefile
Windows平台
cmake -G "MSYS Makefiles" ./
Linux平台
cmake ./
cmake 命令之后目录如下
│ CMakeCache.txt
│ CMakeLists.txt
│ cmake_install.cmake
│ main.c
│ Makefile
│
├─add
│ add.c
│ add.h
│
├─CMakeFiles
│ │ cmake.check_cache
│ │ CMakeDirectoryInformation.cmake
│ │ CMakeOutput.log
│ │ Makefile.cmake
│ │ Makefile2
│ │ progress.marks
│ │ TargetDirectories.txt
│ │
│ ├─3.23.2
│ │ │ CMakeCCompiler.cmake
│ │ │ CMakeCXXCompiler.cmake
│ │ │ CMakeDetermineCompilerABI_C.bin
│ │ │ CMakeDetermineCompilerABI_CXX.bin
│ │ │ CMakeRCCompiler.cmake
│ │ │ CMakeSystem.cmake
│ │ ├─CompilerIdC
│ │ │ │ a.exe
│ │ │ │ CMakeCCompilerId.c
│ │ │ │
│ │ │ └─tmp
│ │ └─CompilerIdCXX
│ │ │ a.exe
│ │ │ CMakeCXXCompilerId.cpp
│ │ │
│ │ └─tmp
│ ├─app.dir
│ │ │ build.make
│ │ │ cmake_clean.cmake
│ │ │ compiler_depend.make
│ │ │ compiler_depend.ts
│ │ │ depend.make
│ │ │ DependInfo.cmake
│ │ │ flags.make
│ │ │ linklibs.rsp
│ │ │ objects1.rsp
│ │ │ progress.make
│ │ │
│ │ ├─add
│ │ ├─multiply
│ │ └─sub
│ └─CMakeTmp
├─multiply
│ multiply.c
│ multiply.h
│
└─sub
sub.c
sub.h
make编译源码
make
生成Windows平台可执行文件 app.exe
或者Linux平台可执行文件 app



