macOS下C++编程

15,024 阅读2分钟

macOS下编写C++代码IDE,可以选择Visual Studio Code或Xcode,推荐使用Xcode,因为使用Visual Studio Code编写C++的前提是,需要安装Xcode。此外Visual Studio Community for Mac不支持C++编程。

本文系统环境:macOS 10.15 Catalina

Visual Studio Code

Visual Studio Code简称VS Code。

  • VS Code下载点击这里。截止到2020年3月9日,VS Code版本为1.43.0。

  • VS Code汉化包可在VS Code商店里搜索:Chinese (Simplified) Language Pack for Visual Studio Code

  • 在VSCode商店里安装C/C++扩展。

VS Code的C/C++扩展不包括C++编译器或调试器,需要在macOS先安装Clang for Xcode,Xcode可以在App Store里安装。在VS Code终端里执行:g++ --help

  • 将VS Code加入到系统PATH环境变量中,下图在VS Code主菜单查看->命令面板...里打开:

  • 新建一个空文件夹,如hellworld,在VS Code里以工作区方式打开:

  • 在命令面板里选择C/C++:编辑配置(UI),会自动生成c_cpp_properties.json文件。

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/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • 在命令面板里选择任务:配置默认生成任务->Others运行任意外部命令的示例,会自动生成task.json文件。

默认生成的task.json文件如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

使用以下内容替换task.json文件:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build with Clang",
      "type": "shell",
      "command": "clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "helloworld.cpp",
        "-o",
        "helloworld.out",
        "--debug"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
  • 在命令面板里选择调试:打开launch.json->C++(GDB/LLDB),会自动生成launch.json文件。

默认生成的launch.json文件如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "输入程序名称,例如 ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

"program": "输入程序名称,例如 ${workspaceFolder}/a.out",

替换为:

"program": "${workspaceFolder}/helloworld.out",

跟task.json文件内的hellowrold.out一致。

  • 添加一个C++源文件helloworld.cpp,示例代码如下:
#include <iostream>

int main() {
    std::cout << "Hello cpp";
    return 0;
}

此时工作区目录结构如下,注意helloworld.cpp的位置跟.vscode位置是同一级的:

  • 在命令面板里选择任务:运行生成任务,或按下快捷组合键⇧⌘B,或选择终端->运行生成任务...,编译运行程序:

  • 启用调试程序:

如果显示以下问题: Warning: Debuggee TargetArchitecture not detected, assuming x86_64,可以在launch.json里增加下面的配置:

"targetArchitecture": "x86_64"

最后的运行结果为:

  • c_cpp_properties.json tasks.json launch.json这3个文件可以复制到其他工作区重复使用,只需要把对应的cpp和out文件名替换即可。

  • 参考引用:code.visualstudio.com/docs/cpp/co…

Xcode

Xcode版本信息如下:

File->New->Project 选择macOS下的Command Line Tool项目模板类型:

Language为C或C++,下图选择C:

选择C,默认生成main.c文件,点击左上方运行按钮Run结果如下:

选择C++,默认生成main.cpp文件,点击左上方运行按钮Run结果如下:

常见问题

  • 问题:*** is not a valid command

解决:项目所在目录和项目名不能是中文,改成英文。