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

    1. {
    2. //tasks.json
    3. "tasks":
    4. [
    5. {
    6. "type": "cppbuild",
    7. "label": "C/C++: g++ build active file",
    8. "command": "/usr/bin/g++",
    9. "args": [
    10. "-fdiagnostics-color=always",
    11. "-g",
    12. "${file}",
    13. "-o",
    14. "${fileDirname}/${fileBasenameNoExtension}",
    15. "-std=c++17",
    16. "-stdlib=libc++",
    17. ],
    18. "options": {
    19. "cwd": "${fileDirname}"
    20. },
    21. "problemMatcher": [
    22. "$gcc"
    23. ],
    24. "group": {
    25. "kind": "build",
    26. "isDefault": true
    27. },
    28. "detail": "compiler: /usr/bin/g++"
    29. }
    30. ],
    31. "version": "2.0.0"
    32. }
    1. {
    2. //c_cpp_properties.json
    3. "configurations": [
    4. {
    5. "name": "Mac",
    6. "includePath": [
    7. "${workspaceFolder}/**"
    8. ],
    9. "defines": [],
    10. "macFrameworkPath": [
    11. "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
    12. ],
    13. "compilerPath": "/usr/bin/g++",
    14. "cStandard": "c17",
    15. "cppStandard": "c++17",
    16. "intelliSenseMode": "gcc-x64"
    17. }
    18. ],
    19. "version": 4
    20. }
    1. {
    2. //launch.json
    3. "configurations": [
    4. {
    5. "name": "C/C++: debug",
    6. "type": "lldb",
    7. "request": "launch",
    8. "program": "${fileDirname}/${fileBasenameNoExtension}",
    9. "args": [],
    10. "cwd": "${workspaceFolder}",
    11. "preLaunchTask": "C/C++: g++ build active file"
    12. }
    13. ],
    14. "version": "2.0.0"
    15. }

    但最新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.
    于是乎我们还是抽出其中的要点, 主要还是三个配置文件:

    1. {
    2. //tasks.json
    3. "tasks":
    4. [
    5. {
    6. "type": "cppbuild",
    7. "label": "C/C++: clang++ build active file",
    8. "command": "/usr/bin/clang++",
    9. "args": [
    10. "-fcolor-diagnostics",
    11. "-fansi-escape-codes",
    12. "-g",
    13. "${file}",
    14. "-o",
    15. "${fileDirname}/${fileBasenameNoExtension}",
    16. "--std=c++17",
    17. ],
    18. "options": {
    19. "cwd": "${fileDirname}"
    20. },
    21. "problemMatcher": ["$gcc"],
    22. "group": {
    23. "kind": "build",
    24. "isDefault": true
    25. },
    26. "detail": "Task generated by Debugger."
    27. }
    28. ],
    29. "version": "2.0.0"
    30. }
    1. {
    2. //c_cpp_properties.json
    3. "configurations": [
    4. {
    5. "name": "Mac",
    6. "includePath": ["${workspaceFolder}/**"],
    7. "defines": [],
    8. "macFrameworkPath": [
    9. "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
    10. ],
    11. "compilerPath": "/usr/bin/clang++",
    12. "cStandard": "c11",
    13. "cppStandard": "c++17",
    14. "intelliSenseMode": "macos-clang-arm64"
    15. }
    16. ],
    17. "version": 4
    18. }
    1. {
    2. //launch.json
    3. "configurations": [
    4. {
    5. "name": "C/C++: clang++ build and debug active file",
    6. "type": "cppdbg",
    7. "request": "launch",
    8. "program": "${fileDirname}/${fileBasenameNoExtension}",
    9. "args": [],
    10. "stopAtEntry": false,
    11. "cwd": "${fileDirname}",
    12. "environment": [],
    13. "externalConsole": false,
    14. "MIMode": "lldb",
    15. "preLaunchTask": "C/C++: clang++ build active file"
    16. }
    17. ],
    18. "version": "2.0.0"
    19. }

    然后在当前c++ file点击右上角的Debug C/C++ File就可以Run了.

  • 相关阅读:
    邮件名称修改和yml里面配置mail方式
    嵌入式开发每天都做什么?
    Windows7 - 永恒之蓝 - 测试
    通信接口五种主要的类型是什么?RS-232、485、CAN、USB
    Spring MVC的常用注解及用法
    Navicat 下载
    EEPROM、FLASH电路设计
    C# 通过Dynamic访问System.Text.Json对象
    thrift
    基于训练和推理场景下的MindStudio高精度对比
  • 原文地址:https://blog.csdn.net/brahmsjiang/article/details/138075835