问题
Visual Studio Code中的插件: Code Runner 支持运行C、C++、Java、JS、PHP、Python等多种语言。
但是它不支持C++11特性的一些使用,比如类似错误:
binarySearch.cpp:26:17: error: non-aggregate type ‘vector’ cannot be initialized with an initializer list
需要安装插件:
- C/C++
- CodeRunner
C/C++配置
打开插件拓展, 找到C/C++, 选择扩展设置:
可通过查找,找到配置Custom Configuration Variables, 并打开setttings.json
文件
添加如下内容,保存关闭
"C_Cpp.default.compilerArgs": ["-g","${file}","-std=c++11","-o","${fileDirname}/${fileBasenameNoExtension}"
],
然后打开本目录内的**.vscode**文件夹,查看是否存在c_cpp_properties.json
文件,如果没有,则新建, 它是用于配置C++语言环境的IntelliSense配置相关,将如下内容复制进去:
{"configurations": [{"name": "Mac","defines": [],"macFrameworkPath": ["/System/Library/Frameworks","/Library/Frameworks","${workspaceFolder}/**"],"compilerPath": "/usr/bin/g++","cStandard": "c11", // 如果存在,则重要修改此处"cppStandard": "c++11", // 如果存在,则重要修改此处"intelliSenseMode": "clang-x64","browse": {"path": ["${workspaceFolder}"],"limitSymbolsToIncludedHeaders": true,"databaseFilename": ""}}],"version": 4
}
然后打开 .vscode 文件夹下的tasks.json
文件,在args
内增加属性:
"args": ["-std=c++11", // add"-stdlib=libc++", // add"-fdiagnostics-color=always", // add"-fcolor-diagnostics","-fansi-escape-codes","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"
],
到这里C++11的配置算是结束,但是CodeRunner插件需要配置下,保证C++11的代码运行正常。
Code Runner
打开拓展,找到CodeRunner,选择扩展设置:
找到Executor Map By File Extension, 打开settings.json
查找下字符串"cpp", 替换内容为:
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
如图所示:
至此配置结束。