在Visual Studio Code macOS上尽量用Clang编译C++

在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个配置文件的一份参考(出处不详):

XML 复制代码
{
    //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"
}    
XML 复制代码
{
    //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
}  
XML 复制代码
{
	 //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.

于是乎我们还是抽出其中的要点, 主要还是三个配置文件:

XML 复制代码
{
    //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"
}
XML 复制代码
{
    //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
}
XML 复制代码
{
    //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了.

相关推荐
qinzechen几秒前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
嵌入式小能手37 分钟前
开发环境搭建之VScode的安装及使用
vscode·编辑器
丶Darling.1 小时前
代码随想录 | Day26 | 二叉树:二叉搜索树中的插入操作&&删除二叉搜索树中的节点&&修剪二叉搜索树
开发语言·数据结构·c++·笔记·学习·算法
小飞猪Jay1 小时前
面试速通宝典——10
linux·服务器·c++·面试
程序猿阿伟2 小时前
《C++高效图形用户界面(GUI)开发:探索与实践》
开发语言·c++
阿客不是客2 小时前
深入计算机语言之C++:C到C++的过度
c++
LN-ZMOI2 小时前
c++学习笔记1
c++·笔记·学习
no_play_no_games2 小时前
「3.3」虫洞 Wormholes
数据结构·c++·算法·图论
￴ㅤ￴￴ㅤ9527超级帅2 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
五味香2 小时前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理