purpose

To learn the forgotten C language, to learn the Linux kernel, and to learn the JVM in depth

start

First I used GCC on Windows compiler, vscode official has summarized helloworld tutorial, here is the link

Installation environment

  1. Install C/C++ plug-ins
  2. Download the mingw-W64 compiler and unzip it. Because the online download area of the official website is extremely slow, so I choose to download the offline package. Here is the download link. After the download, decompress the file and add the bin directory to the environment variable
  3. Command line validation:

Configure the HelloWorld program to run

  1. First, I used vscode to open the project root directory. I created hello-c-projects in my common project directory as my root directory
  2. This root directory will act as a workspace for vscode, which is generated during compilation, debugging, and startup.vscodeFolder for the generated environment configuration files
  3. Write a hello. C
    #include <stdio.h>
    int main(a)
    {
      printf("Hello C Programming !");
    }
    Copy the code
  4. You can useCtrl + Shift + EQuickly locate the currently edited File in the left File Explorer
  5. The IntelliSense plug-in enables vscode to provide c/c++ intelligent syntax hints
  6. Now start compiling the hello.c source file. If you’re compiling a.cpp file, you’ll see three options. Since I’m currently studying.c, that is, the c program does not include C ++, there is only one default gcc.exe.hereSome differences between GCC and g++ are briefly introduced. At this time.vscode/task.jsonThe file is generated:
    {" version ":" 2.0.0 ", "tasks" : [{" type ":" cppbuild ", "label" : "C/C + + : GCC. Exe build active file", // specify the program to run; "D: \ \ packages \ \ c_cpp \ \ x86_64-8.1.0 - release - posix - seh - rt_v6 - rev0 \ \ mingw64 \ \ bin \ \ GCC exe", // specify the command line argument to be passed to GCC.  // `gcc -g D:\hello-c-projects\helloworld\hello.c -o D:\hello-c-projects\helloworld\hello.exe` "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}. Exe ", "options": {" CWD ":" D: \ \ packages \ \ c_cpp \ \ x86_64-8.1.0 - release - posix - seh - rt_v6 - rev0 \ \ mingw64 \ \ bin "}, "problemMatcher" : ["$GCC "], "group": {"kind": "build", "isDefault": true // CTRL + Shift + B will default to this task}, "detail": "compiler: D: \ \ packages \ \ c_cpp \ \ x86_64-8.1.0 - release - posix - seh - rt_v6 - rev0 \ \ mingw64 \ \ bin \ \ GCC exe "}}]Copy the code

    Vscode has an official variable for the tasks.json configuration fileThe documentTo elaborate. Here’s a rough list of what some variables mean:

    • ${workspaceFolder} – the path of the folder opened in VS Code – The absolute path to the folder opened by vscode
    • ${workspaceFolderBasename} – the name of the folder opened in VS Code without any slashes (/) – Name of the folder vscode opens
    • ${file} – the current opened file – Absolute path to the currently opened file
    • ${relativeFile} – the current opened file relative to workspaceFolder – The path of the currently open file relative to the workspace
    • ${relativeFileDirname} – the current opened file’s dirname relative to workspaceFolder – The folder name of the relative path
    • ${fileBasename} – the current opened file’s basename – The name of the file currently open
    • ${fileBasenameNoExtension} – the current opened file’s basename with no file extension – The current open file name, without suffix
    • ${fileDirname} – the current opened file’s dirname – The absolute path to the folder of the currently open file
    • ${fileExtname} – the current opened file’s extension – The file suffix
    • ${cwd} – the task runner’s current working directory on startup – The current working directory of the task runner at startup
    • ${lineNumber} – the current selected line number in the active file – The currently selected line number in the active file
    • ${selectedText} – the current selected text in the active file – The currently selected text in the active file
    • ${execPath} – the path to the running VS Code executable – Path to VS Code executable (code.exe location)
    • ${defaultBuildTask} – the name of the default build task – The name of the default build task
  7. You do not need to modify the configuration file. After selecting the hello.c file, use the shortcut keyCtrl + Shift + BThe default task is run directly, and a Hello.exe executable is compiled. If you don’t debug and start directly, you can run the file directly from the command line. Win10 explorer open double-click can also be executed (but for this program, the command line will flash away just ~).
  8. * Compile multiple files. Win10 platform under the mingw-W64 provides mingw32-make.exe this executable makefile, through writing makefile can achieve multiple file compilation. I will not discuss this for the time being. GCC and Cmake are involved, and I will not do so now
  9. Debug a single file. You need to create a launch.json file in the.vscode folder to support F5 debugging capabilities. Top bar > run > add configuration >C++(GDB/LLDB)> select task. You are advised to modify the label and description configured in tasks.json to distinguish the default tasks provided by the system.
  10. I won’t bother to explain the various fields in launch.json. Here is the document, marked with the anchor point directly click open to read.
  11. The interface for debugging is similar to that of other ides; it is easy to see at a glance. The left-hand column has a “monitor” function that allows you to enter expressions to monitor changes in some variables as the program runs, as shown here
  12. C/C ++ configuration file: This file can be created as follows: [Ctrl + Shift + P] Open command Panel > Edit:C/C++: Edit configuration (JSON)(Seriously, it’s really nice to be able to type in Chinese to launch commands). This will generate a default c_cpp_properties.json configuration file in the.vscode folder. Here are the results generated on my computer:
    {
      "configurations": [{"name": "Win32"."includePath": ["${workspaceFolder}/**"]."defines": ["_DEBUG"."UNICODE"."_UNICODE"]."compilerPath": "D: \ \ packages \ \ c_cpp \ \ x86_64-8.1.0 - release - posix - seh - rt_v6 - rev0 \ \ mingw64 \ \ bin \ \ GCC exe"."cStandard": "gnu17"."cppStandard": "gnu++14"."intelliSenseMode": "gcc-x64"}]."version": 4
    }
    Copy the code

    Mouse over a key can see annotations, very convenient. All items in this file are configured to allow IntelliSense to provide more friendly IntelliSense. For the user, the only thing you need to change isincludePathThis item (when your header file is not in the current workspace). For more information about this configuration file, seehere.

Now you can start to learn C language, learning the process will naturally also be exposed to more GCC, cmake, vscode to use the way, the only need is more time investment.