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

相关推荐
lulu_gh_yu21 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
ULTRA??1 小时前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
HerayChen1 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
hairenjing11231 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
凌云行者1 小时前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者2 小时前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
~yY…s<#>2 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
可均可可3 小时前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
白子寰3 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++