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

{
    //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了.

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Visual Studio Code 中配置 C 环境,你需要安装一些扩展和配置一些设置。下面是一个简单的步骤: 1. 安装 Visual Studio Code:从官方网站下载并安装最新版本的 Visual Studio Code。 2. 安装 C/C++ 扩展:在 Visual Studio Code 中点击左侧的扩展图标,搜索并安装 "C/C++" 扩展,由 Microsoft 提供。 3. 安装 C 编译器:安装一个 C 编译器,比如 GCC 或者 Clang。在 Windows 上,你可以安装 MinGW-w64,并将其路径添加到系统的环境变量中。在 macOS 上,你可以使用 Xcode Command Line Tools 或者安装 Clang。在 Linux 上,你可以使用系统包管理器安装 GCC 或者 Clang。 4. 配置编译任务:在 Visual Studio Code 中按下 `Ctrl + Shift + B`(或者通过菜单 "终端" -> "运行生成任务")来打开任务面板。选择 "C/C++: gcc build active file"(或者其他类似的选项),这将创建一个 `tasks.json` 文件。 5. 配置调试器:在 Visual Studio Code 中点击左侧的调试图标,然后点击顶部的齿轮图标以创建一个 `launch.json` 文件。选择 "C++ (GDB/LLDB)" 或者 "C++ (Windows)"(取决于你的平台),这将创建一个 `launch.json` 文件。 6. 编写和调试代码:在 Visual Studio Code 中创建一个新的 C 文件,或者打开一个已有的 C 文件。通过按下 `F5` 键(或者点击调试面板上的绿色箭头)来开始调试。 这些步骤应该能够帮助你在 Visual Studio Code 中配置 C 环境。你还可以根据自己的需要进一步定制和优化配置。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值