经常使用工具——1.Editor

编辑器

最近一直在用编辑器,记录下Atom 和VS Code两款编辑器编译和调试C++时的注意事项,避免之后忘记了。c++

操做环境Win:git

  • 默认已经安装配置好MinGW/cygwin,可正常使用gcc/g++/gdb/make等命令;
  • 默认已经安装好Atom/VS Code并添加至系统环境变量以方便使用;

Atom

切换至工做目录(这里使用一个简单的测试目录),使用atom打开工做目录:shell

gpp-compiler插件

想要使用Atom来编译运行C++,若文件相对较少,则能够只安装gpp-compiler的Atom插件便可完成编译运行json

主要功能:编辑器

This Atom package allows you to compile and run C++ and C within the editor.ide

快捷按键:测试

To compile C or C++, press F5 or right click the file in tree view and click Compile and Run./按F5来编译并运行c/c++,或者在文件目录上右键选择编译并运行字体

To compile C or C++ and attach the GNU Debugger, press F6 or right click the file in tree view and click Compile and Debug./按F6来编译、调试c/c++,或者在文件目录上右键选择编译并调试ui

linter & linter-gcc 以及其余依赖插件

安装linter & linter-gcc 以及其余依赖插件,以后即可以在Atom中获得语法提示、错误标注等帮助this

Linter plugin for Linter, provides an interface to gcc/g++.

Used with files with grammar "C", "C++" and "C++14".

Now with linting on-the-fly! This is a new feature so please open an issue if you encounter any problems.

GCC Make Run插件

安装完GCC Make Run插件,就可使用make命令来执行工做目录下的Makefile文件,是在Atom中实现多文件编译的主要插件

主要功能:

  • Compile the current actively opened C/C++ file
  • Execute the current actively opened Makefile
  • Execute Makefile from Tree View
  • Customize compilers used
  • Customize compiler flags
  • Customize run options

设置选项:

可使用默认的编译器路径:

或者填写本身安装的路径:

快捷按键:

  • Shortcut to compile-run [ default: f6 ]/按F6来编译并运行
  • Shortcut to access run options panel [ default: ctrl-f6/cmd-f6 ]/按ctrl+F6调出运行选项面板

运行示例:

build & build-make插件

主要功能:

  • Builds your project - configure it your way.
  • Automatically extract targets - here with build-make.(依赖build-make插件)
  • Match errors and go directly to offending code - with Atom Linter.(依赖linter及其余相关依赖插件)

快捷按键:

  • build:select-active-target [ default: f7] call the selector atom-workspace, atom-text-editor/按F7来经过build-make插件,选择select-active-target选项

运行示例:

总结

若是要使用Atom来完成对C++的多文件编译运行能够安装:

  • linter & linter-gcc 以及其余依赖插件,来进行语法、错误提示
  • GCC Make Run插件,使用make命令来执行工做目录下的Makefile文件
  • build & build-make插件,更方便的配置本身build的过程

VS Code

使用Makefile方法来编译项目的好处是可使用不一样的编辑器来编译和运行,即有着能够跨平台好处。

使用VS Code打开相同工做目录:

在VS Code中编译c++须要用到的插件:C/C++ for Visual Studio Code就足够了

另外,须要编写几条json:

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include"    //Notice1:添加include文件夹
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",  //Notice2:gcc路径
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

c_cpp_properties.json配置完成以后,直接的表现便是代码中引入的标准头文件下的绿色波浪线再也不显示。

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
        "type": "cppdbg",
        "request": "launch",
        "name": "Launch GDB",
        "program": "${workspaceFolder}/build/hello.exe",    //程序路径
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "preLaunchTask": "build",               //须要在模板中额外加入预启动任务的label
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",    //gdb路径
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
    {
        "type": "cppdbg",
        "request": "launch",
        "name": "Launch Program Example",
        "program": "${workspaceFolder}/build/hello.exe",    //程序路径
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "preLaunchTask": "build",               //须要在模板中额外加入预启动任务的label
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",    //gdb路径
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
]
}

添加launch配置以后的效果:

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",            //表示执行make命令   
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }

    ]
}

运行示例:

VS code 经常使用快捷键

一、注释:

  • 单行注释:[ctrl+k+c] 或 ctrl+/

  • 取消单行注释:[ctrl+k+u] 或 ctrl+/

  • 多行注释:[alt+shift+A]

  • 多行注释:/**

二、移动行:alt+up/down

三、显示/隐藏左侧目录栏 ctrl + b

四、复制当前行:shift + alt +up/down

五、删除当前行:shift + ctrl + k

六、控制台终端显示与隐藏:ctrl + ~

七、查找文件/安装vs code 插件地址:ctrl + p

八、代码格式化:shift + alt + f (Before use this keyboard shortcut you need to install formatter document for the using language .)

九、新建一个窗口 : ctrl + shift + n

十、行增长缩进: ctrl + [

十一、行减小缩进: ctrl + ]

十二、裁剪尾随空格 : ctrl + shift + x

1三、字体放大/缩小: ctrl + / -

1四、拆分编辑器 : ctrl + 1 / 2

1五、切换窗口 : ctrl + shift + left/right

1六、关闭编辑器窗口 : ctrl + w

1七、关闭全部窗口 : ctrl + k + w

1八、切换全屏 : F11

1九、自动换行 : alt + z

20、显示git : ctrl + shift + g

2一、全局查找文件:ctrl + shift + f

2二、显示相关插件的命令 :ctrl + shift + p

2三、选中文字:shift + left / right / up / down

2四、折叠代码: ctrl + k + 0

2五、展开代码: ctrl + k + j

2六、快速切换主题:ctrl + k / ctrl + t

2七、快速回到顶部 : ctrl + home

2八、快速回到底部 : ctrl + end

2九、格式化选定代码 :ctrl + k / ctrl +f

30、选中代码 : shift + 鼠标左键

3一、多行同时添加内容 :ctrl + alt + up/down

3二、全局替换:ctrl + shift + h

3三、当前文件替换:ctrl + h

3四、打开最近打开的文件:ctrl + r

3五、打开新的命令窗:ctrl + shift + c

REF

https://go.microsoft.com/fwlink/?linkid=830387

https://atom.io/packages/build

小白摸索了一个周末,大佬们有好的方法能够告诉我

相关文章
相关标签/搜索