[For beginners] Introducing the procedure to Hello, World in C/C ++ language using Visual Studio Code on Ubuntu

Summary of this article

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".

Referenced articles

In writing, I referred to the following article.

environment

--OS: Ubuntu 20.04 LTS Japanese

procedure

01. Download VS Code

Download the VS Code .deb file from the URL below with a browser in Ubuntu (firefox is fine).

https://code.visualstudio.com/

image.png

This time it will be used on Ubuntu, so click the icon below to download it. image.png

You will be asked if you want to download it. Select [Save File] and press [OK]. image.png

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
$

02. Install other required packages

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

03. Install VS Code

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

image.png

04. VS Code settings

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 image.png

Type "C/C ++" in the text box to search for the [C/C ++] extension. Click Install to install this plugin.

image.png

Next, install [CMake]. image.png

Next, install [CMake Tools]. image.png

05. Creating a new project

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…] image.png

In the [Open Folder] window, select the folder "develop/hello" you created earlier and press [OK]. image.png

The specified project folder is opened. image.png

From now on, we will set up build and debug. Open the command palette. Menu: [View]-> [Command Palette] image.png

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.

image.png

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.

image.png image.png

Then select "GCC for x86-linux-gnu xxxx" (xxxx depends on the version). image.png

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]. image.png

At the top of the screen, [Enter the name of the new project] is displayed. Enter "hello" as the project name. image.png

Then select Create Executable Executable. image.png

At the bottom right, you will see "CMake Tools is trying to create in this folder ...", so select "Allow". image.png

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. image.png

06. Run the program

To run the program, press F5 or select the Run-> Start Debugging menu. image.png

Choices are displayed at the top of the screen, so select [C ++ (GDB/LLDB)].

image.png

Then select [g ++ 9 Active File…]. (Numbers may vary) image.png

Debugging starts. image.png

Select [TERMINAL] at the bottom of the screen to display the program execution result. You can see that "Hello, world!" Is displayed.

image.png

07. Change the source code file

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. image.png You can delete a file by selecting [main.cpp], right-clicking and selecting [Delete]. image.png

A confirmation of deletion is displayed. Select Move to Trash. image.png

Create a new source file. Right-click on a blank area of ​​the file and select New File. image.png

Enter the file name. The file name is [helloworld.c].

image.png

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. image.png

Press F5 or select Run-> Start Debugging to start debugging and display the results in TERMINAL.

image.png

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.

image.png

08. Where are the project related files?

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

image.png

//////that's all

Recommended Posts

[For beginners] Introducing the procedure to Hello, World in C/C ++ language using Visual Studio Code on Ubuntu
Fastest installation of Visual Studio Code on Ubuntu
Tips for using the Spotify app on Ubuntu
Beginners try using android studio Part 1 (Hello World)
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Installing Visual Studio Code on Ubuntu 20.04 (automatic update support)
I couldn't type Japanese in Ubuntu20.04 + Visual Studio Code
Output settings to debug console in Visual Studio Code
[Introduction] Display Android Studio Hello World on the emulator
Configuration script for using docker in proxy environment on ubuntu 20.04.1
Until Android Studio beginners display "Hello World" on HUAWEI nova 2
Java in Visual Studio Code
How to use PlantUML with Visual Studio Code (created on October 30, 2020)
Challenge to explain Java's Hello World while referring to the language specifications
[RSpec on Rails] How to write test code for beginners by beginners
How to install the language used in Ubuntu and how to build the environment
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Must-see for beginners! Specific method to manage X code project on Github ②
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Farewell to Eclipse, Visual Studio Code for moderately lightweight JavaFX development-Environment construction-
Programming for the first time in my life Java 1st Hello World
(For super beginners) Getter / setter and property to think in D language