一、C++代码示例
//first_cmake.cpp #include<iostream> using namespace std; int main(int argc ,char *argv[]) { cout<<"hello camke!"<<endl; return 0; }
二、CMakeLists.txt编写
#CMakeLists.txt cmake文件名大小写不敏感 #指定cmake最低版本 cmake_minimum_required(VERSION 3.20) #构建项目的名称 project(first_cmake) #构建执行程序 add_executable(first_cmake first_cmake.cpp)
三、文件结构
四、编译
cmake -S . -B n -G "NMake Makefiles"
D:\Project\cmake\20230704\first_cmake>cd n D:\Project\cmake\20230704\first_cmake\n>nmake //或者cmake --build n Microsoft (R) 程序维护实用工具 14.16.27049.0 版 版权所有 (C) Microsoft Corporation。 保留所有权利。 [ 50%] Building CXX object CMakeFiles/first_cmake.dir/first_cmake.cpp.obj first_cmake.cpp ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\iostream ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\ios ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\yvals_core.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xkeycheck.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\crtdefs.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\sal.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\concurrencysal.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vadefs.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\crtdbg.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcruntime_new_debug.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\use_ansi.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\limits.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cstdlib ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\stdlib.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_malloc.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_search.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\stddef.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wstdlib.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\math.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_math.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xtgmath.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cstdio ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\stdio.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wstdio.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_stdio_config.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\streambuf ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cstring ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_memcpy_s.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcruntime_string.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wstring.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\stdexcept ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\exception ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xstddef ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\initializer_list ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\malloc.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcruntime_exception.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_terminate.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xstring ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xmemory0 ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cstdint ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\limits ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\ymath.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cfloat ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\cwchar ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wconio.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wctype.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wdirect.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wio.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wprocess.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\corecrt_wtime.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\sys/stat.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\sys/types.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\new ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\utility ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xatomic0.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\typeinfo ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocinfo ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\locale.h ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xfacet ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\system_error ?⒁? 包含文件: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xerrc.h ?⒁? 包含文件: C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\share.h [100%] Linking CXX executable first_cmake.exe [100%] Built target first_cmake D:\Project\cmake\20230704\first_cmake\n>
推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/VsFMs