一、安装插件
1、安装C/C++插件
2、安装CMake、CMake插件
3、安装其他插件
3.1、Error Lens(错误提示)
3.2、One Dark Pro(主题插件)
二、代码演示
1、main.c
#include <iostream>
int main()
{
std::cout << "\nhello world\n" << std::endl;
return 0;
}
2、CMakeLists.txt
参考CMake官网
2.1 创建文件 CMakeLists.txt
在CMakeLists.txt文件中添加:
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.cxx)
2.2 Ctrl + P打开配置文件
2.3 VScode的Unable to determine what CMake generator to use问题处理
Unable to determine what CMake generator to use. Please install or configure a preferred generator, or update settings.json, your Kit configuration or PATH variable.
解决方案
- 检查当前根目录下(即你通过VSCode的打开文件夹功能打开的文件夹)是否存在以“.code-workspace”文件,如果不存在则如下面的操作创建此文件
然后在弹出来的文件保存管理窗口中直接点“保存”即可。
- 打开“.code-workspace”文件
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.associations": {
"*.ejs": "html",
"iostream": "cpp"
},
"cmake.generator": "MinGW Makefiles"
}
}
改成这个样子即可(添加了划红线的语句,旨在告诉CMake你要用的generator姓甚名谁)
"cmake.generator": "MinGW Makefiles"
3、确保你的VScode至少同时拥有以下3个扩展
- C/C++
- CMake
- CMake Tools
4、然后在工具栏点击CMake
[proc] Executing command: C:\Program_Files_AI\gcc\gcc_1221\ucrt64\bin\gcc.exe -v
[main] Configuring folder: Ctest01
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\Program_Files_AI\gcc\gcc_1221\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\Program_Files_AI\gcc\gcc_1221\ucrt64\bin\g++.exe -Sd:/Workspaces_C++/Ctest01 -Bd:/Workspaces_C++/Ctest01/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 12.2.1
[cmake] -- The CXX compiler identification is GNU 12.2.1
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/Program_Files_AI/gcc/gcc_1221/ucrt64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/Program_Files_AI/gcc/gcc_1221/ucrt64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/Workspaces_C++/Ctest01/build
三、VSCode窗口作用
1、选择“编译器”
2、编译
VScode的Unable to determine what CMake generator to use问题处理_LYZ832的博客-CSDN博客