Compile C++ code in Ubuntu (from installing the virtual machine to completing the compilation)

Table of contents

1. Install the virtual machine

(1)Official website download

(2)Virtual machine installation

2. Create a virtual machine

(1) Download the image file (.iso).

(2) Create a virtual machine

3. Set up the virtual machine

(1) Adjust font size

(2) Chinese version of ubuntu

(3) root initialization

(4) Download vim

4. Compile C++ files

(1) Create a shared folder

(2) Compile C++ files

1. Download cmake

2. Use the touch command to create a new file

3. Use gedit to open CMakeLists and helloworld files

4. Create a workspace and compile

5. Appendix (some useful linux commands)


1. Install the virtual machine

Virtual Machine VMware is a "virtual PC" software that can run two or more systems simultaneously on a single machine.

(1)Official website download

Official website download

Open the official website and select the appropriate version. We choose the Windows version here.

(It is recommended to create a folder in the D drive specifically to store Vmware when downloading)

(2)Virtual machine installation

Click on the exe file you just downloaded. If there is a firewall reminder, just choose to run:

Click next:

Select "I accept" and click "Next":

Select the installation location. It is not recommended to install on the C drive; the second one is checked by default; click Next:

The user experience settings here are optional, but I didn’t choose them:

Select shortcuts, you can check them all here, which is more convenient:

Click Install and wait for the installation to complete:

Click to finish:

2. Create a virtual machine

(1) Download the image file (.iso).

       After the virtual machine is downloaded, it only provides a platform, or it can be understood as a container. At this point, it is like a computer that already has a hard disk and memory, but does not yet have an operating system. So we need to download the program CD image file.

       Ubuntu is one of the famous Linux distributions and is currently the Linux version with the most users . We choose to download Ubuntu. However, since the server is abroad, it will be very slow to download the Ubuntu system from the official website. We can download it from some domestic mirror websites:

1. Tsinghua University open source website mirror station

Tsinghua University open source website

2. Enter ubuntu in the search bar at the top and select ubuntu-releases:

(3) Select an ubuntu version to download. Since the new Ubuntu version will more or less have bugs that have never appeared before, for the convenience of later use, we can find solutions when problems arise. To be on the safe side, it is recommended to download 18.04:

(Supplement: I later upgraded to 20.04. It is recommended that you install 20.04 directly here to avoid the time-consuming subsequent upgrades. Of course, you can install it if you have memory. Downloading 20.04 is the same as downloading the 18.04 version. Here everyone checks "20.04/")

                           

(4) Select the .iso file and wait for download:

             

(The file is a bit large, you can copy the download link to Thunder, and you can create a new ubuntu folder under the VMware file just for storage)

(The following operations are the same for version 18 and version 20, but the custom naming is different. Please pay attention when naming and it will be ok)

(2) Create a virtual machine

1. Click VMWare on the desktop. You need to enter the key license or try it for 30 days. You can go to Baidu to find the solution. It is not convenient to expand here.

After entering the key license, enter the welcome interface:

2. Click to create a new virtual machine

3. You can choose either "Typical" or "Custom". We choose the first one here and click Next:

4. Click "Browse" and select the iso file we just downloaded:

5. The next step is to fill in some personalized information. The password needs to be remembered. This password is the password you need to enter every time you log in to Ubuntu:

6. Customize the virtual machine name and location:

7. The maximum disk defaults to 20G. It does not occupy 20G all at once. You can keep the default.

8. Click Finish to complete the creation of the virtual machine.

(7) After waiting for the download to complete, enter the previous username and password

 

(I don’t know why there is no response when I use the numeric keyboard to input here. If everyone can also input using the numeric keys at the top of the keyboard, it will be fine.)

After logging in successfully, ubuntu is finally ready to use!

3. Set up the virtual machine

(1) Adjust font size

The font size of the virtual machine is too small and it is very tiring to look at. You can adjust the font size by referring to the following method:

1. Right-click to open the terminal and enter the command to update the software source:

sudo apt-get update  

2. Install the gnome-tweaks desktop configuration tool

sudo apt install gnome-tweaks

3. Pop-up optimization window

gnome-tweaks

4. After the window opens, click Fonts and adjust the numbers that suit you:

(2) Chinese version of ubuntu

If you are not familiar with the virtual machine at the beginning, you can choose to translate the virtual machine into Chinese. After you become familiar with it, you can change it back to English.

Reference link

(3) root initialization
sudo passwd root

Only after the initialization is successful can we switch to the super user if necessary.

(4) Download vim

vim is a highly configurable text editor in linux. It may be used frequently in the future use of virtual machines.

sudo apt-get install vim-gtk

4. Compile C++ files

(1) Create a shared folder

       Since the Ubuntu system is installed in a VMware virtual machine, files are often transferred between the two. Therefore, a shared folder is needed . The so-called shared folder is a folder that can be accessed by both the virtual machine and the host. The files in this folder can be operated by both the virtual machine and the host. It is equivalent to a bridge for file interaction.

1. Create a shared folder

Create a shared folder on your computer, named share, which can be created in the previous VMware folder. You can put some files in the share folder to check later whether the shared folder is created successfully.

2. Turn off the virtual machine and open the virtual machine settings at the top

3. Start the virtual machine, open the terminal, and use the following command to check whether there is a folder:

cd /mnt/hgfs/share

4. Check whether you have switched to the share file, and use ls to check whether there are files under the folder.

ls

As shown in the picture above, the shared folder is created successfully.

Later, you will encounter the problem of shared folder failure after the virtual machine is restarted. You can solve it by following the following operations:

1. View the currently mounted shared folder

vmware-hgfsclient

2. If the folder exists, use the following command:

sudo vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other
sudo vmhgfs-fuse .host:/ /mnt/hgfs -o nonempty -o allow_other

3. Set up auto-start at power on:

sudo vi /etc/rc.local 

4. After entering the vi interface, press 'i' to enter editing mode and enter:

#!/bin/sh -e
sudo vmhgfs-fuse .host:/ /mnt/hgfs -o nonempty -o allow_other

After completing the input, press the Esc key to enter the general command mode, enter ":" to enter the command line mode, and finally enter wq to save and exit.

5. Give the file execution permissions

sudo chmod +x /etc/rc.local

6. Restart the virtual machine

reboot

Reference blog post

(2) Compile C++ files

       Let's start with a simple helloworld and use the cmake method to compile the c++ file.

       In fact, we can also use the gcc compiler to compile C++ programs. This is more suitable for situations where there is only one source file. But if our program contains multiple source files, if we use gcc to compile them one by one, it will become very troublesome and the workload will be huge. So we have the batch processing tool: make

       The make tool can be regarded as an intelligent batch processing tool. It does not have the function of compiling and linking itself, but uses a batch-like method to compile and link by calling the user-specified commands in the makefile file .

      And where do we get the makefile file? In some simple projects, we can write it manually, but when facing some complex projects, we can use the CMakeLists.txt file (scientific name: configuration file) to generate the makefile.

       Of course, we need to write the CMakeLists.txt file ourselves.

Summary process:

  1. Writing .cpp files

  2. Write the CMakeLists.txt file, and the CMakeLists.txt file generates the makefile file

  3. Use the cmake command to use the CMakeLists.txt file as the starting point to generate a makefile in the current directory.

  4. Use the make command to call user-specified commands in the makefile file to compile and link.

  5. ./: Run related programs

Reference link

1. Download cmake

a. Update g++

sudo apt-get install g++ 

If you encounter locking problems, such as:

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

Try closing the terminal or entering the following command:

sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock

b. Check whether there is cmake

which cmake

c. If the path is displayed, it means cmake is included and uninstall it.

sudo apt-get remove cmake

d. Download cmake

wget https://www.cmake.org/files/v3.10/cmake-3.23.0.tar.gz

e. Unzip cmake

tar -zxvf cmake-3.23.0.tar.gz

f. Enter the decompressed cmake folder and check

cd cmake-3.23.0

g. Execute the installation process

./bootstrap

Error condition 1 occurs:

Error when bootstrapping CMake: Cannot find appropriate Makefile processor on this system. Please specify one using environment variable MAKE

input the command:

sudo apt-get install build-essential

Error condition 2 occurs:

CMake Error at Utilities/cmcurl/CMakeLists.txt:454 (message): Could not find OpenSSL. Install an OpenSSL development package or configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.

input the command:

apt-get install libssl-dev

Reference blog post

After resolving the error, check again:

./bootstrap

h. Compilation structure

make

I. Installation

sudo make install

J. Check

cmake -version    //检查版本
which cmake       //检查位置

cmake is installed successfully!

(Additional: Whichever version you download here will be displayed. The 3.10 version is still too low. It is recommended that you download a higher cmake version)

Reference blog post

2. Use the touch command to create a new file
touch CMakeLists.txt
touch helloworld.cpp
3. Use gedit to open CMakeLists and helloworld files

Open the CMakeLists.txt file:

gedit CMakeLists.txt

Enter in the CMakeLists file:

#cmake最低版本号要求
cmake_minimum_required(VERSION 2.8)
#工程名
project(HELLOWORLD)
#包含原程序,即把给定目录下的源程序复制给变量DIR_SRC
#将指定路径下的源文件储存在指定的变量中
#下面这句话,有些博客中写错了,需要注意
aux_source_directory(./ DIR_SRC )
#生成程序
add_executable(HELLOWORLD  ${DIR_SRC})

Open helloworld.cpp:

gedit helloworld.cpp

Enter in the helloworld.cpp file:

#include<iostream>
int main()
{
    std::cout<<"hello world!"<<std::endl;
    return 0;
}

The name of CMakeLists.txt here must be entered correctly, otherwise an error will be reported when cmake cannot find the file!

4. Create a workspace and compile
//创建目录:mkdir 
//切换目录:cd
//cmake .. :使用CMakeLists.txt 文件作为起始点在当前目录中生成makefile
//make : 调用makefile文件中用户指定的命令来进行编译和链接
//  ./HELLOWORD : 运行helloword文件
mkdir build 
cd build
cmake ..
make
./HELLOWORD 

Finally, running the c++ file under the ubuntu file was successful!

5. Appendix (some useful linux commands)

In addition to the linux commands mentioned above, there are many commonly used linux commands, such as:

  1. cd directory: switch to the specified directory

  2. cd ~: Switch to the home directory of the current directory

  3. cd ..: switch to the previous directory

  4. cd -: switch to the previous directory

  5. rm -rf: Delete directories recursively downward without prompting

  6. tab: Sometimes the file name is too long, you can use the tab key to complete it

  7. ls: View the contents of the current directory

Guess you like

Origin blog.csdn.net/chenhuifei/article/details/131615501