[Configure vscode to write C or C++ programs, and output the program on the external console and solve the problem of Chinese garbled characters! ! ! 】

Configure vscode to write C or C++ programs, and output the program on the external console and solve the problem of Chinese garbled characters! ! !

1. Click Extensions and add the plug-ins required for C language (click Install, and after the installation is complete, click the Set icon to install another version. Be sure to install version 1.8.4. This version can automatically generate the configuration files required for code running), After completion, reload or close vscode and reopen it.

Insert image description here

2. Download and install the compiler MinGW and system variable configuration

https://sourceforge.net/projects/mingw-w64/files/

参数选择:
①64位电脑选择x8664;32位电脑选择686
②写的C语言程序运行在windows下选择win32;
运行在其它操作系统下选择posix(这是一个协议,windows.不遵循)
③64位电脑,seh比较新但不支持32位;sjj支持32位稳定性好,
推荐选择seh,因为在安装程序中默认就是选seh。
④32位电脑,dwarf性能更优但不支持64位。

Insert image description here

After downloading to local area, unzip it to any location (do not include Chinese characters in the path), copy the bin directory path to configure system variables

Enter the advanced system settings, open environment variables, find path in system variables, and paste the copied path into the new variable
Insert image description here

Insert image description here

Then open the command line and enter gcc -v. If a lot of codes appear, it means the environment configuration is successful. If it prompts that this is an illegal command, it means the configuration failed.

Insert image description here

3. After the installation is complete, use vscode to open any folder to save the code (it is best not to have Chinese characters in the path and an error may be reported)

Create a new file in the folder with .c as the suffix

Insert image description here

Enter the following sample code

#include<stdio.h>
int main(void){
    
    
    printf("Hello World!");
    printf("你好啊!");
    system("pause");//可以在外部控制台打开
    return 0;
}

Then press F5 or click Run to run the code (debugging or not)

Insert image description here

After clicking Run, click the first C++ [GDB/LLDB] in the pop-up window

Insert image description here

After clicking, select the local compiler in the pop-up window.

Insert image description here

Then you will find that a .vscode folder will be automatically generated in the folder, with two json files in it. The debugging console below will output the following content, and print out the content to be output in the code in the terminal.

Insert image description here
Insert image description here

At this point, you can create and write C language code files normally in the folder!

4. Configuration optimization: output programs on the external console and solve Chinese garbled characters

To output the program to the external console, you need to add system("pause"); then open the launch.json file and change "externalConsole": false in the figure below to "externalConsole": true,

Insert image description here

After modification, run the c language file again. The running results are as follows. You will find that the program output is on the external console, but the Chinese characters will be garbled.

Insert image description here

Open the task.json file in the .vscode folder, find "${fileDirname}\${fileBasenameNoExtension}.exe", add an English comma at the end and press Enter to the next line, paste the following text "-fexec-charset= GBK"and save

Insert image description here

After saving, run the program again as shown below. The problem of Chinese garbled characters has been solved!

Insert image description here

Guess you like

Origin blog.csdn.net/qq_62124267/article/details/133712869