Cmake入门使用
Makefile#
跟我一起写Makefile - 跟我一起写Makefile 1.0 文档
CMake#
cmake和autotools正是makefile的上层工具,它们的目的正是为了产生可移植的makefile,并简化自己动手写makefile时的巨大工作量。如果你自己动手写过makefile,你会发现,makefile通常依赖于你当前的编译平台,而且编写makefile的工作量比较大,解决依赖关系时也容易出错。因此,对于大多数项目,应当考虑使用更自动化一些的 cmake或者autotools来生成makefile,而不是上来就动手编写。
🌟cmake 就是用来产生 Makefile 的工具,解析 CMakeLists.txt 自动生成 Makefile。
Windows下Cmake安装步骤详解(图文)_L_Li_L的博客-CSDN博客_windows安装cmake
cmake快速入门_kavin.zhu的博客-CSDN博客_cmake
在Win10系统上使用VScode + Cmake配置C/C++开发环境,实现一键编译运行_To_be_a_fisher的博客-CSDN博客
实验#
1. 写CmakeLists.txt#
# cmake最低版本需求
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
# 指定工程名
PROJECT(CMake_Test)
# 把当前目录(.)下所有源代码文件和头文件加入变量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
# 生成应用程序helloworld
ADD_EXECUTABLE(helloworld ${SRC_LIST})
2. 进行cmake,生成Makefile#
D:\zxy\cpp_test\CMake_Test\build>cmake ../
-- Building for: NMake Makefiles
CMake Error at CMakeLists.txt:4 (PROJECT):
Running
'nmake' '-?'
failed with:
系统找不到指定的文件。
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "D:/zxy/cpp_test/CMake_Test/build/CMakeFiles/CMakeOutput.log".
解决:将命令改为cmake -G "MinGW Makefiles" ../
,即加入generater参数。
D:\zxy\cpp_test\CMake_Test\build>cmake -G "MinGW Makefiles" ../
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Programs/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Programs/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/zxy/cpp_test/CMake_Test/build
3. make命令进行编译#
D:\zxy\cpp_test\CMake_Test\build>mingw32-make
[ 50%] Building CXX object CMakeFiles/helloworld.dir/helloworld.cpp.obj
[100%] Linking CXX executable helloworld.exe
[100%] Built target helloworld
*由于没有把mingw64/bin中的mingw32-make.exe改名为make.exe,需要输入全名才能使用make。
build/中会生成一个helloworld.exe可执行文件。
4. 运行helloworld.exe#
D:\zxy\cpp_test\CMake_Test\build>.\helloworld.exe
Hello, World
5. 总结#
/*项目文件结构*/
CMake_Test
├─ build
│ ├─ CMakeCache.txt
│ ├─ CMakeFiles
│ │ ├─ 3.25.0-rc3
│ │ │ ├─ 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
│ │ ├─ cmake.check_cache
│ │ ├─ CMakeDirectoryInformation.cmake
│ │ ├─ CMakeOutput.log
│ │ ├─ CMakeScratch
│ │ ├─ helloworld.dir
│ │ │ ├─ build.make
│ │ │ ├─ cmake_clean.cmake
│ │ │ ├─ compiler_depend.make
│ │ │ ├─ compiler_depend.ts
│ │ │ ├─ depend.make
│ │ │ ├─ DependInfo.cmake
│ │ │ ├─ flags.make
│ │ │ ├─ helloworld.cpp.obj
│ │ │ ├─ helloworld.cpp.obj.d
│ │ │ ├─ link.txt
│ │ │ ├─ linkLibs.rsp
│ │ │ ├─ objects.a
│ │ │ ├─ objects1
│ │ │ └─ progress.make
│ │ ├─ Makefile.cmake
│ │ ├─ Makefile2
│ │ ├─ pkgRedirects
│ │ ├─ progress.marks
│ │ └─ TargetDirectories.txt
│ ├─ cmake_install.cmake
│ ├─ helloworld.exe
│ └─ Makefile
├─ CMakeLists.txt
├─ helloworld.cpp
└─ README.md
分类:
搬砖日记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?