最简单的CMake示例

本文将介绍如何使用CMake构建简单的项目。通过一个基础的CMakeLists.txt文件配置,理解CMake的基本语法和流程,适用于初学者快速上手CMake。

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

PS E:\workspace\structsample\ostack> ls

    目录: E:\workspace\structsample\ostack

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020-07-17      0:10                build
-a----       2020-07-17      0:04            198 CMakeLists.txt
-a----       2020-07-15     23:45            147 main.cpp
-a----       2020-07-15     23:43            740 ostack_array.cpp
-a----       2020-07-15     23:43            419 ostack_array.h

PS E:\workspace\structsample\ostack> cat .\CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(vscode_cmake_Test)
aux_source_directory(. DIR_TOOT_SRCS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
add_executable(${PROJECT_NAME} ${DIR_TOOT_SRCS})
PS E:\workspace\structsample\ostack>
PS E:\workspace\structsample\ostack\build> cmake -G"Unix Makefiles" ../
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: D:/Program Files/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/Program Files/MinGW/bin/gcc.exe -- 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: D:/Program Files/MinGW/bin/c++.exe -- 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: E:/workspace/structsample/ostack/build
PS E:\workspace\structsample\ostack\build> ls


    目录: E:\workspace\structsample\ostack\build


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020-07-17      0:09                CMakeFiles
-a----       2020-07-17      0:09          15992 CMakeCache.txt
-a----       2020-07-17      0:09           1451 cmake_install.cmake
-a----       2020-07-17      0:09           6104 Makefile


PS E:\workspace\structsample\ostack\build> make
Scanning dependencies of target vscode_cmake_Test
[ 33%] Building CXX object CMakeFiles/vscode_cmake_Test.dir/main.cpp.obj
[ 66%] Building CXX object CMakeFiles/vscode_cmake_Test.dir/ostack_array.cpp.obj
[100%] Linking CXX executable vscode_cmake_Test.exe
[100%] Built target vscode_cmake_Test
PS E:\workspace\structsample\ostack\build> ls


    目录: E:\workspace\structsample\ostack\build


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020-07-17      0:10                CMakeFiles
-a----       2020-07-17      0:09          15992 CMakeCache.txt
-a----       2020-07-17      0:09           1451 cmake_install.cmake
-a----       2020-07-17      0:09           6104 Makefile
-a----       2020-07-17      0:10          65787 vscode_cmake_Test.exe


PS E:\workspace\structsample\ostack\build> .\vscode_cmake_Test.exe
Hello word.
PS E:\workspace\structsample\ostack\build>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值