gcc编译到makefile到CMakeLists.txt编译详解

CMake是一个跨平台的构建系统,用于管理C/C++项目的编译流程。相较于gcc和makefile,CMake提供了一种更高级的、平台无关的方式来定制编译过程。通过CMakeLists.txt文件,开发者可以定义项目结构和编译规则,然后生成适合不同平台的本地化构建文件。本文详细介绍了CMake在Windows下的安装步骤和基本使用方法,包括单文件及多文件项目的配置示例,旨在帮助开发者更好地理解和应用CMake。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译C/C++项目,我们一般是通过gcc来编译或者使用IDE来帮我们管理编译环境。

一般来说,使用gcc来编译会比使用IDE更加灵活,并且gcc有着广泛的环境支持,因此我们必须要掌握使用gcc来编译C/C++项目。

但是单纯使用gcc编译,当源文件数目少时还可以管理,当源文件数很多是,各种依赖会使得编译命令特别多,特别复杂,不易进行管理。

因此我们一般会通过GNU套件里提供的make工具,我们通过编写C/C++项目的makefile来帮我们管理编译过程。makefile的教程在此:gcc和makefile用法总结(建议收藏)

通过撰写makefile,我们可以只需要执行 make 命令,就可以生成最后的目标文件或可执行文件。

其实上面的这个步骤对于编译C/C++项目已经是很好了,但还存在一些缺点。

  1. makefile的编写还是有点麻烦,其实makefile里面的语句就是我们gcc要执行的语句,所以源文件一多,编写makefile还是有点麻烦的。
  2. makefile的编写和所在硬件和系统的平台有关,不方便移植。

cmake就是为了解决上面两个问题而解决的,cmake允许开发者编写一种平台无关的 CMakeList.txt 文件来定制整个编译流程,然后再根据目标用户的平台进一步生成所需的本地化 Makefile 和工程文件,如 Unix 的 Makefile 或 Windows 的 Visual Studio 工程。从而做到“Write once, run everywhere”。显然,CMake 是一个比上述几种 make 更高级的编译配置工具。

这篇文章gcc和makefile用法总结(建议收藏)已经讲了gcc和makefile的使用方法,这篇文章就主要讲一下cmake的使用方法。


windows下cmake的安装

CMake 是一个开源的跨平台的自动化构建系统,主要用于 C++ 的工程构建、测试以及打包等自动化操作。它能根据开发者编写的 CMakeLists.txt 规则文件,在不同的平台输出所需要的工程文件,然后开发者可以按照常见的构建方式使用生成的工程文件编译最终的程序。

CMake 是 Cross platform Make 的缩写,虽然名字中含有“make”,但是 CMake 和 Linux 上常见的 make 系统有所区别,是更上一层的构建系统。

安装 CMake 很简单,可以直接去其官网下载对应的二进制文件,如果 Linux 平台,可以直接使用包安装工具直接安装。下面主要介绍 Windows 平台下的安装详情。

首先打开 CMake 官方网站,在下载页面 选择 Windows win64-x64 Installer 下载:
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210722215513187.png

下载完成直接双击安装:

image-20210722215541810

在安装选项的页面中,最好将 CMake 加入到环境变量,以便于以后在命令行中可以直接使用 cmake 命令。

image-20210722215609114

最后直接选择 Install 按钮安装即可。

在这里插入图片描述

安装完成后,打开 cmd 命令行工具,输入make -version查看结果,如果显示相应的版本号即安装成功。

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210722215731654.png

cmake的使用教程

单文件CMakeLists.txt写法

下面来一个简单示例,演示一下怎么使用cmake

//源文件 main.c
#include <stdio.h> 
int main(int argc, char* argv[]){ 
    printf("hello world\n");
    return 0; 
} 
//CMakeLists.txt
cmake_minimum_required(VERSION 3.10) 

# set the project name 
project(Main) 

# add the executable 
add_executable(Main main.c) 

在Windows下,cmake默认生成的是VisualStudio的项目windows cmake没有生成Makefile,我们使用命令

cmake . -G "Unix Makefiles"

就可以生成makefile了,下面是makefile的内容

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.21

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Disable VCS-based implicit rules.
% : %,v

# Disable VCS-based implicit rules.
% : RCS/%

# Disable VCS-based implicit rules.
% : RCS/%,v

# Disable VCS-based implicit rules.
% : SCCS/s.%

# Disable VCS-based implicit rules.
% : s.%

.SUFFIXES: .hpux_make_needs_suffix_list

# Command-line flag to silence nested $(MAKE).
$(VERBOSE)MAKESILENT = -s

#Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = D:/CMake/bin/cmake.exe

# The command to remove a file.
RM = D:/CMake/bin/cmake.exe -E rm -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = E:/wenjian/cs/code/cmake/testCmake

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = E:/wenjian/cs/code/cmake/testCmake

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
	D:/CMake/bin/cmake-gui.exe -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
	D:/CMake/bin/cmake.exe --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
	$(CMAKE_COMMAND) -E cmake_progress_start E:/wenjian/cs/code/cmake/testCmake/CMakeFiles E:/wenjian/cs/code/cmake/testCmake//CMakeFiles/progress.marks
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
	$(CMAKE_COMMAND) -E cmake_progress_start E:/wenjian/cs/code/cmake/testCmake/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named Main

# Build rule for target.
Main: cmake_check_build_system
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 Main
.PHONY : Main

# fast build rule for target.
Main/fast:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/build
.PHONY : Main/fast

main.obj: main.c.obj
.PHONY : main.obj

# target to build an object file
main.c.obj:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/main.c.obj
.PHONY : main.c.obj

main.i: main.c.i
.PHONY : main.i

# target to preprocess a source file
main.c.i:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/main.c.i
.PHONY : main.c.i

main.s: main.c.s
.PHONY : main.s

# target to generate assembly for a file
main.c.s:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/main.c.s
.PHONY : main.c.s

# Help Target
help:
	@echo "The following are some of the valid targets for this Makefile:"
	@echo "... all (the default if no target is provided)"
	@echo "... clean"
	@echo "... depend"
	@echo "... edit_cache"
	@echo "... rebuild_cache"
	@echo "... Main"
	@echo "... main.obj"
	@echo "... main.i"
	@echo "... main.s"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

下面我们执行

make

就可以根据makefile生成可执行文件了。继续执行

./Main.exe

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210722221648908.png

同一目录,多个源文件

上面是一个非常简单的例子,源文件只有一个,实际项目会有非常多的源文件,也会调用很多的库文件,可能头文件包含的路径也会增加。

下面我们的源文件增加一个

//main.c
#include <stdio.h> 
#include "call.h"

int main(int argc, char* argv[]){ 
    printf("hello world\n");
    callfunction();
    return 0; 
} 

//call.c
#include <stdio.h>

void callfunction(void) {
    printf("call a function\n");
}

//call.h
extern void callfunction(void);

CMakeLists.txt 就要改成下面这样了

cmake_minimum_required(VERSION 3.10) 

# set the project name 
project(Main) 

# add the executable 
add_executable(Main main.c call.c) 

继续执行下面的命令

 cmake . -G "Unix Makefiles"
 make
 ./Main.exe

image-20210722223844781

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机载软件与适航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值