在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了.

相关推荐
渝妳学C9 分钟前
【C++】类和对象(下)
c++
EleganceJiaBao24 分钟前
【C语言】结构体模块化编程
c语言·c++·模块化·static·结构体·struct·耦合
Narutolxy27 分钟前
在 macOS 和 Windows 平台上使用 SVN 的完整指南20241225
windows·macos·svn
liutaiyi835 分钟前
Redis可视化工具 RDM mac安装使用
redis·后端·macos
xianwu54339 分钟前
反向代理模块。开发
linux·开发语言·网络·c++·git
scoone41 分钟前
VSCode 性能优化指南:提高编码效率,减少资源占用
ide·vscode·编辑器
Bucai_不才1 小时前
【C++】初识C++之C语言加入光荣的进化(上)
c语言·c++·面向对象
木向1 小时前
leetcode22:括号问题
开发语言·c++·leetcode
筑基.1 小时前
basic_ios及其衍生库(附 GCC libstdc++源代码)
开发语言·c++
yuyanjingtao1 小时前
CCF-GESP 等级考试 2023年12月认证C++三级真题解析
c++·青少年编程·gesp·csp-j/s·编程等级考试