Visual Studio Code使用CMake

本文介绍了如何在VisualStudioCode中配置CMake并创建一个简单的项目,包括Windows环境下的两种生成可执行程序的方法:通过VS项目和MinGWMakefiles。详细步骤涉及CMakeLists.txt的编写和项目结构管理。
摘要由CSDN通过智能技术生成

title: Visual Studio Code使用CMake
author: XinPing Tu
date: 2024-04-21 14:05:15
update: 2024-04-22 12:56:20
language: zh-CN
categories:

  • CMake
    tags:
  • Visual Studio Code
  • CMake
  • Visual Studio
  • MinGW

📢 声明:
转载,请先标注出处哦!编写不易,尊重一下劳动成果哦!
个人博客网站 ==》https://alicewanttobackuw.github.io/
github ==》https://github.com/AliceWantToBackUw
csdn ==》https://blog.csdn.net/lengyue29

csdn会自动转存图片,使用img已无效,为了更好的阅读体验,请移步至Visual Studio Code使用CMake

2024-04-22更新:添加了3.2目录下的删除操作文章的超链接

Visual Studio Code使用CMake

1 前置条件

  • 安装CMake:通过cmake -version检查有无安装成功
  • 安装MinGW(目录3.2需要):参考文章http://t.csdnimg.cn/qByEV

2 创建一个简单项目

//-------head.h
#ifndef _HEAD_H
#define _HEAD_H
// 加法
int add(int a, int b);
// 减法
int subtract(int a, int b);
// 乘法
int multiply(int a, int b);
// 除法
double divide(int a, int b);
#endif


//-------add.cpp
#include <stdio.h>
#include "head.h"

int add(int a, int b)
{
  return a + b;
}


//-------div.cpp
#include <stdio.h>
#include "head.h"

double divide(int a, int b)
{
  return (double)a / b;
}



//-------mul.cpp
#include <stdio.h>
#include "head.h"

int multiply(int a, int b)
{
  return a * b;
}



//-------sub.cpp
#include <stdio.h>
#include "head.h"

// 减法
int subtract(int a, int b)
{
  return a - b;
}



//-------main.cpp
#include <stdio.h>
#include "head.h"

int main()
{
  int a = 20;
  int b = 12;
  printf("a = %d, b = %d\n", a, b);
  printf("a + b = %d\n", add(a, b));
  printf("a - b = %d\n", subtract(a, b));
  printf("a * b = %d\n", multiply(a, b));
  printf("a / b = %f\n", divide(a, b));
  return 0;
}

项目结构

03cmake
├─ .vscode
│  ├─ c_cpp_properties.json
│  └─ tasks.json
├─ add.cpp
├─ CMakeLists.txt
├─ div.cpp
├─ head.h
├─ main.cpp
├─ mult.cpp
└─ sub.cpp

3 Windows下生成可执行程序

3.1 通过生成vs项目,再由vs编译生成而得

参考文章:http://t.csdnimg.cn/zQP57

在windows环境下,在根目录使用下列命令,会默认生成Visual Studio项目工程文件

cmake .

此时的项目结构变成了

03cmake
├─ .vscode
│  ├─ c_cpp_properties.json
│  └─ tasks.json
├─ 03make.vcxproj
├─ 03make.vcxproj.filters
├─ add.cpp
├─ ALL_BUILD.vcxproj
├─ ALL_BUILD.vcxproj.filters
├─ CMakeCache.txt
├─ CMakeFiles
│  ├─ 3.29.0-rc4
│  │  ├─ CMakeCCompiler.cmake
│  │  ├─ CMakeCXXCompiler.cmake
│  │  ├─ CMakeDetermineCompilerABI_C.bin
│  │  ├─ CMakeDetermineCompilerABI_CXX.bin
│  │  ├─ CMakeRCCompiler.cmake
│  │  ├─ CMakeSystem.cmake
│  │  ├─ CompilerIdC
│  │  │  ├─ CMakeCCompilerId.c
│  │  │  ├─ CompilerIdC.exe
│  │  │  ├─ CompilerIdC.vcxproj
│  │  │  ├─ Debug
│  │  │  │  ├─ CMakeCCompilerId.obj
│  │  │  │  ├─ CompilerIdC.exe.recipe
│  │  │  │  └─ CompilerIdC.tlog
│  │  │  │     ├─ CL.command.1.tlog
│  │  │  │     ├─ CL.read.1.tlog
│  │  │  │     ├─ CL.write.1.tlog
│  │  │  │     ├─ CompilerIdC.lastbuildstate
│  │  │  │     ├─ link.command.1.tlog
│  │  │  │     ├─ link.read.1.tlog
│  │  │  │     └─ link.write.1.tlog
│  │  │  └─ tmp
│  │  ├─ CompilerIdCXX
│  │  │  ├─ CMakeCXXCompilerId.cpp
│  │  │  ├─ CompilerIdCXX.exe
│  │  │  ├─ CompilerIdCXX.vcxproj
│  │  │  ├─ Debug
│  │  │  │  ├─ CMakeCXXCompilerId.obj
│  │  │  │  ├─ CompilerIdCXX.exe.recipe
│  │  │  │  └─ CompilerIdCXX.tlog
│  │  │  │     ├─ CL.command.1.tlog
│  │  │  │     ├─ CL.read.1.tlog
│  │  │  │     ├─ CL.write.1.tlog
│  │  │  │     ├─ CompilerIdCXX.lastbuildstate
│  │  │  │     ├─ link.command.1.tlog
│  │  │  │     ├─ link.read.1.tlog
│  │  │  │     └─ link.write.1.tlog
│  │  │  └─ tmp
│  │  ├─ VCTargetsPath.txt
│  │  ├─ VCTargetsPath.vcxproj
│  │  └─ x64
│  │     └─ Debug
│  │        ├─ VCTargetsPath.recipe
│  │        └─ VCTargetsPath.tlog
│  │           └─ VCTargetsPath.lastbuildstate
│  ├─ c1c397eb2ba1589aadad67e99cf541fa
│  │  └─ generate.stamp.rule
│  ├─ cmake.check_cache
│  ├─ CMakeConfigureLog.yaml
│  ├─ CMakeScratch
│  ├─ generate.stamp
│  ├─ generate.stamp.depend
│  ├─ generate.stamp.list
│  ├─ pkgRedirects
│  └─ TargetDirectories.txt
├─ CMakeLists.txt
├─ cmake_install.cmake
├─ demo.sln
├─ div.cpp
├─ head.h
├─ main.cpp
├─ mult.cpp
├─ README.md
├─ sub.cpp
├─ ZERO_CHECK.vcxproj
└─ ZERO_CHECK.vcxproj.filters

你需要用Visual Studio来打开.sln项目文件,然后运行,才能够生成可执行程序。
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

生成成功,你可在项目根路径查看到debug目录,其中有个03make.exe(你自己在CMakeLists.txt设置名字)可执行程序
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

回到Visual Studio,将03make项目设为启动项目
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

点击本地Windows 调试器即可
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

3.2 通过生成makefiles文件,再由MinGW编译而得

参考文章:http://t.csdnimg.cn/m9JqW

首先,先清理一下上面创建vs工程文件。由于之前使用直接在项目根目录使用cmake命令(即cmake .),因此所生成的vs工程文件,也会和原先的代码杂糅在一起。所以,只能够手动删除,保留源码和CMakeLists.txt
至于如何高效的删除,请阅读这篇文章如何优雅的删除Cmake生成的文件?

以下是删除后的项目结构

03cmake
├─ .vscode
│  ├─ c_cpp_properties.json
│  └─ tasks.json
├─ add.cpp
├─ CMakeLists.txt
├─ div.cpp
├─ head.h
├─ main.cpp
├─ mult.cpp
├─ README.md
└─ sub.cpp

在项目根目录输入以下命令

cmake -S ./ -B ./ -G "MinGW Makefiles"

使用该命令可以直接生成makefiles文件外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

然后可以通过以下命令,在项目根目录中调用MinGW里面中mingw32-make.exe程序编译生成。(这里我重命名为了make.exe

make

即可在当前目录下,生成可执行文件03make.exe
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

运行即可

.\03make.exe

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值