I will introduce the procedure to build a C/C ++ development environment with the Linux version of Visual Studio Code (VS Code) and display "Hello, world". I will introduce you step by step, so it is hard to trip. For beginners. This is a setting method for those who want to easily build a C/C ++ development environment and those who are studying C/C ++ at school.
If you want to set it from the beginning, go to "01. Download VS Code". For those who want to create a new project, see "05. Creating a new project", and for adding / deleting files, see "07. Modifying the source code file".
In writing, I referred to the following article.
--OS: Ubuntu 20.04 LTS Japanese
Download the VS Code .deb file from the URL below with a browser in Ubuntu (firefox is fine).
https://code.visualstudio.com/
This time it will be used on Ubuntu, so click the icon below to download it.
You will be asked if you want to download it. Select [Save File] and press [OK].
The file will be downloaded. Start a terminal and check.
$ ls
Download Desktop Video Music
Template document picture publication
$cd download/
$ ls
code_1.52.1-1608136922_amd64.deb
$
Install "build-essential" required for GCC compilation and build, "cmake" that handles make, and other necessary packages.
$ sudo apt update
$ sudo apt install -y build-essential
$ sudo apt install -y cmake
$ sudo apt install -y apt-transport-https
The downloaded file can be found in "Downloads" under the logged-in user's home folder.
$ cd
$ ls
Download Desktop Video Music
Template document picture publication
$cd download/
$ ls
code_1.52.1-1608136922_amd64.deb
Then install VS Code and the "apt-transport-https" package.
$ sudo apt install ./code_1.52.1-1608136922_amd64.deb
Once installed, you can start VS Code with the "code" command.
$ cd
$ code
Apply extensions to VS Code. There are three extensions to install: "C/C ++", "CMake", and "CMake Tools". First, install the "C/C ++" extension.
Open menu: View-> Extensions
Type "C/C ++" in the text box to search for the [C/C ++] extension. Click Install to install this plugin.
Next, install [CMake].
Next, install [CMake Tools].
C/C ++ programs are created on a project-by-project basis. Projects are created for each folder. First, create a folder and make various settings for that folder.
Create a folder. The folder is "develop/hello" (create a folder called "hello" under "develop").
$ cd
$ mkdir -p develop/hello
Open the created project folder with VSCode. Open menu: [File]-> [Open Folder…]
In the [Open Folder] window, select the folder "develop/hello" you created earlier and press [OK].
The specified project folder is opened.
From now on, we will set up build and debug. Open the command palette. Menu: [View]-> [Command Palette]
The command palette input field appears at the top of the screen. Select CMake: Quick Start. Enter "CMake" in the text box to narrow down the candidates.
When selected, [Configure project ...?] At the bottom right of the screen. ], [Do you always configure when you open the project? ] Is displayed one after another, so select [Yes]. It seems that it will close if you lean on it, so press it early.
Then select "GCC for x86-linux-gnu xxxx" (xxxx depends on the version).
After selecting "GCC for x86…", [CMake… cannot be found in the lower right corner. How do you want to continue? ] Is displayed, so press [Create].
At the top of the screen, [Enter the name of the new project] is displayed. Enter "hello" as the project name.
Then select Create Executable Executable.
At the bottom right, you will see "CMake Tools is trying to create in this folder ...", so select "Allow".
The necessary files will be generated. A list of files is displayed on the left side of the screen. Double-click [main.cpp] to see the sample source code. If [main.cpp] is not displayed, click on the upper left of the screen to display the list.
To run the program, press F5 or select the Run-> Start Debugging menu.
Choices are displayed at the top of the screen, so select [C ++ (GDB/LLDB)].
Then select [g ++ 9 Active File…]. (Numbers may vary)
Debugging starts.
Select [TERMINAL] at the bottom of the screen to display the program execution result. You can see that "Hello, world!" Is displayed.
I decided to delete the sample source code and create a new file for debugging.
You can return to the list of files by clicking the icon below on the left side of the screen. You can delete a file by selecting [main.cpp], right-clicking and selecting [Delete].
A confirmation of deletion is displayed. Select Move to Trash.
Create a new source file. Right-click on a blank area of the file and select New File.
Enter the file name. The file name is [helloworld.c].
Once the file is created, enter the code. The code is as follows.
#include <stdio.h>
int main(void){
printf("Hello, Visual Studio Code\n");
return(0);
}
When you enter the code, the code is completed.
Press F5 or select Run-> Start Debugging to start debugging and display the results in TERMINAL.
Next, change the code as follows.
#include <stdio.h>
int main(void){
int val;
printf("Hello, Visual Studio Code\n");
printf("input value: ");
scanf("%d", &val);
printf("value is %d\n", val);
return(0);
}
Press F5 or select Run-> Start Debugging to start debugging and display the results in TERMINAL. You can also input scanf () directly on TERMINAL.
These files are stored in the folder of the first project you created. Open a terminal and check these files.
$ cd
$ cd develop/hello
$ ls
CMakeLists.txt build helloworld helloworld.c main
If you change the file name of the source code file, it will be reflected in Visual Studio Code.
$ mv helloworld.c helloworld2.c
$ ls
CMakeLists.txt build helloworld helloworld2.c main
//////that's all
Recommended Posts