Linux Ubuntu command line to quickly configure the C++ development environment

  This article introduces a simple method to quickly configure a C++ code development environment for editing, compiling, and running in the Ubuntu version of the Linux operating system based on the command line .

  In the previous article, how to configure Visual Studio Code and C++ code development environment under Linux operating system Ubuntu 22.04 (https://blog.csdn.net/zhebushibiaoshifu/article/details/127037709), we have introduced how to configure Visual Studio Code and C++ code development environment under Linux Ubuntu operating system , how to configure Visual Studio Code software and C++ code development environment. However, our article at that time used Visual Studio Code as the editor , so it was relatively troublesome to configure; and the method at that time used a lot of graphical interface software to operate. For some computers without graphical interfaces (such as servers) ) is inoperable. So here we will introduce how to quickly configure the C++ code development environment based on the command line .

  In order to configure the C++ code development environment, first, we need to configure the compiler . A compiler is a tool that translates source code into machine executable code . It receives source code files as input and converts them into executable files , library files , or other forms of object files ; Linux is generally used gccand used g++as the default Compiler, for Visual Studio , its C++ compiler is Microsoft C++ ( MSVC ).

  First, we enter and run the following two lines of code respectively to check whether the sum exists on the gcccomputer g++.

g++ --version
gcc --version

  Run the above code as shown below. Since I have already configured both here, the specific versions of both will be displayed.

  As you can see, the images above and below show their respective versions.

  If you do not configure gccand g++, then first update the package list through the following code .

sudo apt update

  Run the above code as shown below.

  Then, run the following two lines of code respectively. Still have to pay attention, because I have already configured the sum here gcc, g++so running these codes may be inconsistent with the specific content displayed.

sudo apt install g++
sudo apt install gcc

  Run the first sentence of code above, as shown in the figure below.

  Run the second sentence of code above, as shown in the figure below.

  Next, we need to configure makeand cmake. Both are tools for building software projects , used to automate the compilation and construction process; simply put, if we only have one .cppcode file, which contains the main function, then we can directly use the previously configured gccand g++compile; but For dealing with complex projects, multiple source files, external library dependencies, etc., you need to makeuse cmakeit.

  Similarly, execute the following codes respectively to check the versions of the two on the computer.

make --version
cmake --version

  Run the above code as shown below. I have already configured both here, so the following content appears.

  If you have not configured it before, just execute the following code.

sudo apt install cmake make

  Run the above code as shown below.

  Next, we configure the code editor . In the article How to configure Visual Studio Code and C++ code development environment in the Linux operating system Ubuntu 22.04 (https://blog.csdn.net/zhebushibiaoshifu/article/details/127037709) we also mentioned that configuring the code in the Linux operating system The most complicated thing about the development environment is that it cannot install integrated IDE software such as Visual Studio . Instead, it needs to configure editor software such as Visual Studio Code and other compilers separately . In that article, we used Visual Studio Code with a graphical interface as the editor, so here we choose Vim , a software that does not require a graphical interface, as the editor. If you need to configure Vim on a Mac computer , you can check out the article on how to download and install Vim on a Mac computer: MacVim (https://fkxxgis.blog.csdn.net/article/details/132352344).

  Similarly, first check whether Vim is available on your computer .

vim --version

  Run the above code as shown below.

  Still the same, if you don’t have Vim , just install it through the first sentence of the code below; if you have it but want to see if you can update Vim , just execute the second sentence.

sudo apt install vim
sudo apt upgrade vim

  I run the second sentence of code above here, as shown in the picture below.

  Next, we can write the code. First, create a file in the current working directory.cpp with the following code .

vim test.cpp

  You can note here that you can check the current working directory through the first sentence of the code below; you can check what files or folders are in the current directory through the second sentence of the code below.

pwd
ls

  Run the above code as shown below.

  Let's continue with the previous article. .cppAfter entering the file, we can start writing code. Here we just write a simple print Hello.

  After saving the file, use the following code to compile and link .cppthe source code file, and convert it into an executable file ; where my_hellois the name of the executable file , you can modify it as you like.

g++ test.cpp -o my_hello

  Execute the above code, as shown in the figure below. You can see that if there is an error in the code (I entered a semicolon missing in the code at the beginning), it will prompt the error content; if there is no error, nothing will happen.

  Next, use the following code to execute the executable file we just obtained my_hello.

./my_hello

  Executing the above code means executing our .cppcode; as shown in the figure below, you can see that it has been printed Hello.

  Therefore, it shows that we have successfully configured the C++ code development environment.

  At this point, you're done.

Welcome to follow: Crazy Learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/133006231