美文网首页
macOS使用VSCode编辑Cpp(c++)

macOS使用VSCode编辑Cpp(c++)

作者: crazy一笑 | 来源:发表于2023-08-11 08:37 被阅读0次

1.安装Command Line Tools

1.如果你本身是macOS或者iOS开发者,一般已经在AppStore下载了Xcode,这时候不需要再安装
2.如果没有安装Xcode,也可以去AppStore下载安装Xcode,但是Xcode至少有10几G的大小,所以如果为了少占空间,可以去开发者网站Command Line Tools单独下载
安装完成,打开终端:g++ --version

xxx@192 ~ % g++ --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
xxx@192 ~ % 

显示Apple clang version 14.0.3 (clang-1403.0.22.14.1)代表没问题,14.0.3版本可能有所差异

2.安装VSCode

VSCode,如果下载速度太慢,也可以去搜一些网盘等下载。

3.打开VSXCode安装必要的扩展

  • c/c++


    vscode c cpp.png
  • code runner


    vscode code runner.png

4.创建c++文件,并编译运行

vscode cpp.png
  • 1.进入工程目录,没有的话可以先创建个文件夹打开。
  • 2.创建hello.cppc++文件。
  • 3.编写一段简单的cpp代码。
  • 4.终端使用g++执行g++ hello.cpp -o hello.out -W -Wall -g,将hello.cppc++文件编译成可执行文件hello.out
  • 5.编译成功会在本目录下生成hello.out可执行文件以及dSYM等调试文件。
  • 6.执行./hello.out,运行可执行文件
  • 7.输出结果Hello cpp!%,运行成功。

5.更方便的调试,代码自动化

vscode setting json set for cpp.png
上面下载的code runner,通过配置可以更方便的管理和调试c++项目
  • 1.更改setting设置会自动生成.vscode目录和setting.json文件
  • 2.如果不知道哪里修改,可以自己新建.vscode目录和setting.json文件
  • 3.配置setting.json文件
    • 隐藏dSYM和.out文件
    "files.exclude": {
        // dSYM文件具有调试信息,普通使用的话不看到它就可以了
        "**/*.dSYM": true,
        "**/*.out": true,
    },
  • 自动运行cpp
"code-runner.executorMap": {
    "cpp": "g++ $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
  },

完整的setting.json 文件,包含其他配置,配置完成以后,就不再需要第4步的运行了,直接点击编译器的三角运行按钮,或者使用Ctrl Opt M快捷键,或者在文件编辑右键使用Run Code选项,即可直接查看运行结果。
说明配置中-std=c++17,表示使用c++的17标准版本,可以不添加,也可以根据自己的需要设置版本。

{
 //font size
    "editor.fontSize": 16,
    // 添加希望被忽略的文件,这样一些文件虽然存在于当前工作目录下,但是不会被显示在左侧的文件浏览器里
    "files.exclude": {
        // dSYM文件具有调试信息,普通使用的话不看到它就可以了
        "**/*.dSYM": true,
        "**/*.out": true,
    },
    // --------------------------------------------------------------------------------------
  // Code Runner
  // To run code:
  //   use shortcut "Ctrl Opt N" *
  //   or press F1 and then select/type Run Code,
  //   or right click the Text Editor and then click Run Code in editor context menu
  //   or click Run Code button in editor title menu
  //   or click Run Code button in context menu of file explorer
  // To stop the running code:
  //   use shortcut "Ctrl Opt M" *
  //   or press F1 and then select/type Stop Code Run
  //   or right click the Output Channel and then click Stop Code Run in context menu
  "code-runner.executorMap": {
    // Introduction:
    //   Make sure the executor PATH of each language is set in the environment variable.
    //   You could also add entry into "code-runner.executorMap" to set the executor PATH.
    // Supported customized parameters:
    //   $workspaceRoot: The path of the folder opened in VS Code
    //   $dir: The directory of the code file being run
    //   $fullFileName: The full name of the code file being run
    //   $fileName: The base name of the code file being run, that is the file without the directory
    //   $fileNameWithoutExt: The base name of the code file being run without its extension
    /* ------ 编译、运行只有一个文件的cpp文件 ------ */
    // 注:路径中有空格不会出现问题
    "cpp": "g++ $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // 其中 $fullFileName 是绝对路径,是主文件
    // 自己决定是否加入 && rm $dir\"$fileNameWithoutExt\"\".out\"(也可以添加"files.exclude")
    /* ------ 编译、运行多个cpp文件 ------ */
    // "cpp": "g++ $fullFileName <file_to_link> -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // <file_to_link>的写法:
    //   一般的,你也可以直接写绝对路径
    //     \"/path/xxxx.cpp\"
    //   如果你链接的cpp文件和主文件在一个目录下:
    //     $dir\"xxxx.cpp\"
    //   更一般的,如果你链接的cpp文件不和主文件在一个目录下,需要从当前VSCode的工作目录补充相对路径从而形成绝对路径:
    //     $workspaceRoot\"relative/path/xxxx.cpp\"
    /* ------ 编译c文件 ------ */
    "c": "gcc $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // "c": "gcc $fullFileName <file_to_link> -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c17 && $dir\"$fileNameWithoutExt\"\".out\"",
  },
  // Whether to clear previous output before each run (default is false):
  "code-runner.clearPreviousOutput": true,
  // Whether to save all files before running (default is false):
  "code-runner.saveAllFilesBeforeRun": false,
  // Whether to save the current file before running (default is false):
  "code-runner.saveFileBeforeRun": true,
  // Whether to show extra execution message like [Running] ... and [Done] ... (default is true):
  "code-runner.showExecutionMessage": true, // cannot see that message is you set "code-runner.runInTerminal" to true
  // Whether to run code in Integrated Terminal (only support to run whole file in Integrated Terminal, neither untitled file nor code snippet) (default is false):
  "code-runner.runInTerminal": true, // cannot input data when setting to false
  // Whether to preserve focus on code editor after code run is triggered (default is true, the code editor will keep focus; when it is false, Terminal or Output Channel will take focus):
  "code-runner.preserveFocus": false,
  // Whether to ignore selection to always run entire file. (Default is false)
  "code-runner.ignoreSelection": true,
  // --------------------------------------------------------------------------------------

}

相关文章

网友评论

      本文标题:macOS使用VSCode编辑Cpp(c++)

      本文链接:https://www.haomeiwen.com/subject/mdlzpdtx.html