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

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,
  // --------------------------------------------------------------------------------------

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容