Windows/Mac平台下Visual Studio Code C++开发环境搭建
第一部分:Windows平台(Mingw32+Visual Studio Code)
发现使用Mingw32有一个缺陷,很多c++11的功能都无法编译。更不要说后续的C++14和C++17了。难道Windows下只能用微软自家的编译器?
参考了知乎上这篇文章
https://www.zhihu.com/question/30315894
决定使用Mingw-W64和CLang来搭建开发环境参考第二部分。
一,安装必要组件
Visual Studio Code x64
https://code.visualstudio.com/docs/?dv=win
Git
https://git-scm.com/download/win
Windows平台下(使用Visual Studio自带的cc或者Mingw32-gcc)
这里使用mingw32-gcc/g++
https://sourceforge.net/projects/mingw/files/
二,环境配置
1)配置MingGW的环境变量
参考这篇博文
http://blog.sina.com.cn/s/blog_a67a99d70101kwy3.html
Path中添加Mingw的工具集:
C:\MinGW\bin
添加C和CPP的头文件默认路径
添加lib库路径
2)配置Visual Studio Code
首先安装C/C++扩展插件
接着配置Vscode的中的编译环境。参考文档(https://code.visualstudio.com/docs/languages/cpp)
配置包含库:
首先尝试写一个test.cpp
然后包含stdio.h会提示差找不到头文件stdio.h这时候打开c_cpp_properties.json
配置如下,根据你时机安装的MinGw环境来配置。
这是本机的C/C++依赖头的配置。
"C:\\MinGW\\include",
"C:\\MinGW\\lib\\gcc\\mingw32\\4.9.3\\include",
"C:\\MinGW\\lib\\gcc\\mingw32\\4.9.3\\include\\c++",
"C:\\MinGW\\lib\\gcc\\mingw32\\4.9.3\\include\\c++\\mingw32",
"C:\\MinGW\\lib\\gcc\\mingw32\\4.9.3\\include\\c++\\backward"
(主要是给VSCode的只能感知和语法分析查找依赖关系使用)
接着配置Vscode的一键build(有时候频繁测试修改代码的时候一键编译还是比每次都要你手动敲编译命令快的)
配置task.json
选择Shift+Command+P 并选择Tasks:Configure Tasks.选择从其他模板生成task.json
然后编辑如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build test cpp",
"type": "shell",
"command": "g++",
"args" :[
"-g", "test.cpp"
]
}
]
}
注意cpp文件的路径。
接着选择Ctrl+Shift+B可以进行一键build操作。会自动扩展task.json
以后可以使用快捷键Ctrl+Shift+B来一键build这个任务。
(这个还是不太