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

相关推荐
Betty’s Sweet2 分钟前
[C++]:IO流
c++·文件·fstream·sstream·iostream
敲上瘾16 分钟前
操作系统的理解
linux·运维·服务器·c++·大模型·操作系统·aigc
不会写代码的ys22 分钟前
【类与对象】--对象之舞,类之华章,共绘C++之美
c++
兵哥工控24 分钟前
MFC工控项目实例三十二模拟量校正值添加修改删除
c++·mfc
长弓聊编程34 分钟前
Linux系统使用valgrind分析C++程序内存资源使用情况
linux·c++
cherub.42 分钟前
深入解析信号量:定义与环形队列生产消费模型剖析
linux·c++
暮色_年华1 小时前
Modern Effective C++item 9:优先考虑别名声明而非typedef
c++
重生之我是数学王子1 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
soulteary1 小时前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
我们的五年1 小时前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习