The road to C++ learning (1): Building a C++ development environment

foreword

In the process of learning the Android source code, especially in the Framework and its lower layers, I often encounter some C++ codes, resulting in a smattering of the Android source code; on the other hand, NDK development will also encounter some C++ libraries. Learn C++. Not much nonsense, let’s start now~ (The learning tutorial comes from the C++ tutorial The Cherno (Science Online) by former Electronic Arts (EA) game engine engineer Cherno . There are many translations of this video on station B. Thanks to Cherno and the translator The selfless contribution of the author~)

1. Building C++ environment under Windows platform

Development under Windwos is generally done in VS Code and Visual Studio. Let me talk about the construction of Visual Studio first.

1.1 Visual Studio installation

The official address of Visual Studio: Visual Studio
finds the download option through the link above, and then selects the "Community" version, which is the so-called community version.
Visual Studio

At this time, a downloader for Visual Studio will be downloaded. After clicking, it will be downloaded and installed automatically. After the installation is completed, it is actually a Visual Studio Installer program. You need to install your Visual Studio through this program. Open Visual Studio Installer, you can see the following :
Visual Studio Installer
Since we mainly focus on C++ learning, here we choose to use C++ desktop development . After choosing, there is a very important option here is the installation location , because Visual Studio will choose to install on the C drive by default, which may lead to insufficient capacity of the C drive.
installation path
I choose to install on the D drive, IDE, download cache, shared component tools and SDK should be set up.

Click Install again to install Visual Studio 2022 (I have already installed it here, so the install button is not displayed). After waiting for a while, the installation is complete, and then the project can be created.

1.2 Create a C++ project

After opening Visual Studio 2022, click File, New, and Project, as shown in the figure below:
New Project
After creating a new project, you will see the following interface:
create new project
Select an empty project, click "Next", and enter the "Configure New Project" interface
configure new project
to fill in the project name and project location , click the Create button, and start to create the source file, as shown in the figure below: After
new item
selecting the new item , select **C++ file (.cpp)** and name the cpp file name, as shown in the figure below:
add new item
After clicking Add, you will see "Solution" on the left You can see Main.cpp in the source file in , and you can happily code from here. The test code is as follows:

#include<iostream>

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

The above code is the classic output "hello world" code, click the green triangle above the file to execute the running program.
run
Running result:
hello world!
At this point, we have completely completed the whole process of IDE from installation to writing and running.

2. Construction of C++ environment under Linux platform

The above completed the construction of the C++ environment under Windwos, and then started to build C++ under Linux. The OS I am using is Ubuntu version 16.04.

2.1 Install compilation tools

To build a C++ compilation environment, you need to use g++, cmake, and vim, and download and install them through the apt-get command.

sudo apt-get install cmake g++ vim

Sometimes administrator privileges may be required during installation, so sudo is added in front of the apt-get command. Use the following command to check whether the installation is complete.

g++ -v
vim -v
cmake -version

2.2 cmake script writing

When the above software is installed, you can use cmake to compile C++. I create a project in the home directory, then create src in the project folder to store C++ files, and create a CMakeLists.txt file at the same time.
CMakeLists.txt file:

# cmake的最低(高)版本的要求
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)

project (project)

# PROJECT_SOURCE_DIR CMakeLists.txt的所在目录路径
# 设置library输出目录
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/output")

# 设置可执行文件输出目录
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/output")

# 设置C++编译选项
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14")

# 自定义变量,设置PROJECT_SOURCE_DIR/src
set (SOURCE_PATH "${PROJECT_SOURCE_DIR}/src")

# 打印语句
message(STATUS "C++ output path  ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

# 打印语句
message(STATUS "C++ options ${CMAKE_CXX_FLAGS}")

# 告知cmake源文件是SOURCE_PATH目录下后缀名为.cpp的文件
file (GLOB source_files "${SOURCE_PATH}/*.cpp")

# 生成可执行的hello文件
add_executable(hello ${source_files})

# 生成so动态链接库,Android NDK开发用的比较多
#add_library(hello SHARED ${source_files})

Then create main.cpp in the src directory and enter the code:

#include<iostream>

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

In this way, the cmake and C++ files are written, and the next step is to execute the cmake command.
The command is as follows:

cmake .
make

After entering the command, output will be generated in the current directory of CMakeLists.txt, find the hello file in the output directory, use ./output/helloor ./helloexecute, and print out "hello world!"

Finish

On the Windwos platform, use "the first IDE in the universe" Visual Studio to develop C++ and facilitate debugging at the same time. On the Linux platform, use the cmake script to compile C++. The following C++ development is still based on Visual Studio, learning the core syntax of C++.
As for the C++ development under the Linux platform, vim plus various plug-ins will be considered as the IDE in the future. At the same time, due to the relationship between cmake and NDK development, I will learn cmake scripts systematically later. Let's encourage each other~

Guess you like

Origin blog.csdn.net/RQ997832/article/details/123758874