VS code C/C + + environment configuration (the first step for novices)

Keywords: C C++ Visual Studio Code mingw

When I started to review the C language recently, I thought of getting a hand-in-hand development tool. I remembered my painful experience of installing Visual Studio and decided to choose a lighter VS code. However, after downloading the VS code, it can not be used directly. I need to configure the environment. Here is my personal method for configuring the VS code C/C + + environment

catalogue

1, Installing mingw

  one point one          After downloading, open and download the necessary files (four in total):

  one point two          Configure environment variables:

  one point three          Check for successful installation:

2, Install VS code

two point one          Install the necessary extensions:  

  two point two          Multi file binding: Makefile

two point three          test case


1, Installing mingw

VS code itself is an editor and cannot compile code. To run C/C + + code, first install the compiler and download the link:

https://sourceforge.net/projects/mingw/https://sourceforge.net/projects/mingw/

  one point one          After downloading, open and download the necessary files (four in total):

  • Mingw32 GCC bin (C language compiler)
  • mingw32-gcc-g++-bin(C + + compiler)
  • Mingw32 GDB bin (debug compiled file)
  • Mingw32 make bin (for multi file binding of makefile)

 

After selecting, click installation apply changes. The download is very slow. Wait slowly

 

  one point two          Configure environment variables:

Search the computer taskbar for "environment variables"

 

  Find "path" in the system variable and double-click to open it

Add the bin file address under the mingw installation path

 

  one point three          Check for successful installation:

After win+r cmd, enter gcc -v on the command line to display the version number, indicating that the installation is successful

 

2, Install VS code

The installation process is the same as mingw. There is nothing to pay special attention to. Just install it according to your usual habits. Link:

https://code.visualstudio.com/https://code.visualstudio.com/

two point one          Install the necessary extensions:  

Simplified Chinese: replace with Chinese interface (English interface is recommended)

  C/C + +: intelligent prompt, error prompt

After installing this plug-in, the basic compilation and debugging settings will be automatically configured when writing the. c file for the first time. There is one place that needs to be modified:

In launch.json

"externalConsole": true

Modify this configuration to make the results displayed through the console more intuitive

  two point two          Multi file binding: Makefile

When multiple. c files are compiled together, you need to create your own makefile or tell VS code all file paths. Makefile is recommended here

Click the drop-down list after the plus sign shown in the figure to create a new powershell

Tell the compiler all the. c files used to generate an. exe

gcc .\practice5.5.1.c .\practice5.5.c -o lianbian.exe

Here, paste my. c file and. h file below, or write it yourself

two point three          test case

. c Documents:

//Multi file binding (makefile)
/* 
PS D:\c_code>  gcc .\practice5.5.1.c .\practice5.5.c -o lianbian.exe //Tell the compiler all the. c files used to generate an. exe
PS D:\c_code> .\lianbian.exe
 */
//Merge two strings together
#include "head.h"

void main()
{
    char str_given1[] = "hello";
    char str_given2[] = "world";
    char str_goal[100] = {0};
    int len = strlen(str_given1);
    char *p_goal = &str_goal[len];

    copy(str_given1, str_goal);
    copy(str_given2, p_goal);

    printf("%s\n", str_goal);

    system("pause");
}
void copy(char *str1, char *str2)//The function operates the original array by the pointer (changing the element directly through the address through the dereference of the pointer)
{
    while (*str1)
    {
        *str2 = *str1;
        str1++;
        str2++;
    }
}

. h file

#ifndef HEAD_H
#define HEAD_H

//Header file
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//Macro definition

// Function declaration
void copy(char *str1, char *str2);

// type definition
#endif

Then run the exe file

.\lianbian.exe

The result will output "Hello world" (beginning with classic)

  Well, start your programming journey!

Posted by Lucnet on Tue, 21 Sep 2021 00:44:58 -0700