五、windows上vscode构建c/c++环境

1、安装vscode

官网下载界面:https://code.visualstudio.com/Download

  • 请根据电脑系统安装所需版本点击下载链接(一般情况下点击windows按钮即可)
  • 鼠标左键双击,即可运行安装程序,点击【确认】;
  • 选择安装路径;
  • 创建桌面快捷方式;
  • 添加安装路径到系统环境变量;
  • 等待安装完成即可。

2、安装插件

  • 打开vscode,快捷键Ctrl+Shift+X进入插件市场安装插件
  • C/C++
  • clangd
  • Cmake、Cmake Tools
  • Doxygen
  • GBKtoUTF8

2.1 Cmake配置

快捷键Ctrl+,快速打开vscode设置界面,点击右上方【Open Settings (UI)】进入settings.json全局默认配置文件,添加如下设置

"cmake.pinnedCommands": [

"workbench.action.tasks.configureTaskRunner",

"workbench.action.tasks.runTask"

],

// 配置cmake3.31.0路径window系统

// "cmake.cmakePath": "D:\\Program Files\\CMake\\bin\\cmake.exe",

"cmake.showOptionsMovedNotification": false,

"cmake.showConfigureWithDebuggerNotification": false,

// 配置 CMake 构建目录

"cmake.buildDirectory": "${workspaceFolder}/build",

// 配置 CMake 构建环境变量,使其生成编译命令数据库相当于

// CMakeLists.txt文件里set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

"cmake.buildEnvironment": {

"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"

},

2.2、配置clangd

"clangd.arguments": [

"--background-index",

"--log=verbose",

"--clang-tidy",

// window配置

"--query-driver=D:/Program Files/LLVM/bin/bin/clang++",

// "--query-driver=/usr/bin/gcc,/usr/bin/g++",

"--compile-commands-dir=${workspaceFolder}",

"--header-insertion=never",

"--all-scopes-completion",

"--function-arg-placeholders",

"--pretty"

],

// clangd头文件,系统文件查询

"clangd.fallbackFlags": [

"-ID:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",

"-ID:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32"

],

4、c/c++项目配置

在当前工作区创建.vscode文件夹并新建c_cpp_properties.json和settings.json配置文件

4.1.c_cpp_properties.json配置

{

"configurations": [

{

"name": "Win32",

"includePath": [

"${workspaceFolder}/**",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",

"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"

],

"defines": [

"_DEBUG",

"UNICODE",

"_UNICODE"

],

"windowsSdkVersion": "10.0.17763.0",

"compilerPath": "D:/Program Files/mingw64/bin/g++.exe",

"cStandard": "c17",

"cppStandard": "c++17",

"intelliSenseMode": "gcc-x64"

}

],

"version": 4

}

注意【选项】MinGW-w64获取头文件路径方式:按快捷键Win+R,输入cmd回车,gcc -v -E -x c++ -,如下图示。

4.2 settings.json配置

{

"cmake.cmakePath": "D:\\Program Files\\CMake\\bin\\cmake.exe",

"clangd.path": "D:\\Program Files\\LLVM\\bin\\clangd.exe",

}

上述主要配置cmake和clangd在windows的安装路径 。

4.3 .clangd文件配置

当前工作区新建.clangd文件,具体配置如下:

CompileFlags:

Add:

[

-Xclang,

-Wno-unused-variable, #移除未使用的变量警告

-ID:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include,

]

4.4 CMakeLists.txt配置

具体配置如下:

cmake_minimum_required(VERSION 3.10)

project(my VERSION 1.0)

set(CMAKE_C_STANDARD 17)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(

${PROJECT_SOURCE_DIR}/include

)

add_library(hellworld src/helloworld.c)

add_executable(MyExecutable src/main.c)

target_link_libraries(MyExecutable hellworld)

5、总结

根据上述的步骤,即可完成C/C++在vscode上进行开发。

相关推荐
因特麦克斯19 分钟前
如何实现序列化和反序列化?如何处理对象的生命周期管理?
c++
Hera_Yc.H25 分钟前
C++之二:类和对象
开发语言·c++
毒丐1 小时前
VirtualBox使用教程
java·服务器·c++
小卡拉米本米1 小时前
VSCode常见报错:unins000.exe
vscode
葟雪儿2 小时前
海思芯片 交叉编译curl
linux·运维·服务器·开发语言·c++·嵌入式硬件
Octopus20772 小时前
【C++】红黑树(万字)
c++·笔记·学习
落笔映浮华丶2 小时前
C++(进阶) 第4章 map和set的使⽤
c++
毒丐3 小时前
FreeBSD安装教程
c语言·c++
9毫米的幻想3 小时前
【Linux系统】—— 软件下载
linux·运维·服务器·c语言·c++