在linux上惯用g++编译cpp. 照理说macOS只要装了g++, vscode装了C/C++的扩展包:

此外配置了下列文件就可以用g++编译:
tasks.json (compiler build settings)
launch.json (debugger settings)
c_cpp_properties.json (compiler path and IntelliSense settings)
下列是用于g++对以上3个配置文件的一份参考(出处不详):
- {
- //tasks.json
- "tasks":
- [
- {
- "type": "cppbuild",
- "label": "C/C++: g++ build active file",
- "command": "/usr/bin/g++",
- "args": [
- "-fdiagnostics-color=always",
- "-g",
- "${file}",
- "-o",
- "${fileDirname}/${fileBasenameNoExtension}",
- "-std=c++17",
- "-stdlib=libc++",
- ],
- "options": {
- "cwd": "${fileDirname}"
- },
- "problemMatcher": [
- "$gcc"
- ],
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "detail": "compiler: /usr/bin/g++"
- }
- ],
- "version": "2.0.0"
- }
- {
- //c_cpp_properties.json
- "configurations": [
- {
- "name": "Mac",
- "includePath": [
- "${workspaceFolder}/**"
- ],
- "defines": [],
- "macFrameworkPath": [
- "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
- ],
- "compilerPath": "/usr/bin/g++",
- "cStandard": "c17",
- "cppStandard": "c++17",
- "intelliSenseMode": "gcc-x64"
- }
- ],
- "version": 4
- }
-
- {
- //launch.json
- "configurations": [
- {
- "name": "C/C++: debug",
- "type": "lldb",
- "request": "launch",
- "program": "${fileDirname}/${fileBasenameNoExtension}",
- "args": [],
- "cwd": "${workspaceFolder}",
- "preLaunchTask": "C/C++: g++ build active file"
- }
- ],
- "version": "2.0.0"
- }
但最新macos vscode(如1.88.1)对于g++的支持似乎不那么友好, 每次试图compile或读取配置会报: Cannot find g++ build and debug active file.
很可能因为launch.json的预制配置并没有"g++ build and debug active file", 直接run的话大概率会无法编译.
参看vscode官方doc, 他还是推荐使用clang在macos编译c++: Configure VS Code for Clang/LLVM on macOS.
于是乎我们还是抽出其中的要点, 主要还是三个配置文件:
- {
- //tasks.json
- "tasks":
- [
- {
- "type": "cppbuild",
- "label": "C/C++: clang++ build active file",
- "command": "/usr/bin/clang++",
- "args": [
- "-fcolor-diagnostics",
- "-fansi-escape-codes",
- "-g",
- "${file}",
- "-o",
- "${fileDirname}/${fileBasenameNoExtension}",
- "--std=c++17",
- ],
- "options": {
- "cwd": "${fileDirname}"
- },
- "problemMatcher": ["$gcc"],
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "detail": "Task generated by Debugger."
- }
- ],
- "version": "2.0.0"
- }
- {
- //c_cpp_properties.json
- "configurations": [
- {
- "name": "Mac",
- "includePath": ["${workspaceFolder}/**"],
- "defines": [],
- "macFrameworkPath": [
- "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
- ],
- "compilerPath": "/usr/bin/clang++",
- "cStandard": "c11",
- "cppStandard": "c++17",
- "intelliSenseMode": "macos-clang-arm64"
- }
- ],
- "version": 4
- }
- {
- //launch.json
- "configurations": [
- {
- "name": "C/C++: clang++ build and debug active file",
- "type": "cppdbg",
- "request": "launch",
- "program": "${fileDirname}/${fileBasenameNoExtension}",
- "args": [],
- "stopAtEntry": false,
- "cwd": "${fileDirname}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "lldb",
- "preLaunchTask": "C/C++: clang++ build active file"
- }
- ],
- "version": "2.0.0"
- }
然后在当前c++ file点击右上角的Debug C/C++ File就可以Run了.