How to build a C++ development environment on Linux

If a worker wants to do a good job, he must first sharpen his tools! If we want to develop C++ programs on Linux, we must first set up its development environment.

install linux

There are many distribution versions of Linux, and each has its own favorite. You can install whichever you like. I installed Kali Linux here.Please add a picture description

Install development tools

Next we install some necessary tools:

  • vim : text editing tool
  • g++: C++ compiler
  • CodeLite: C++ IDE development environment
  • CMake: a system tool to manage the compilation process

Reference: https://cmake.org/overview
We can install all these tools with one command:

kali$ sudo apt-get install vim g++ codelite cmake

write a demo

  • Create a project directory:
kali$ mkdir -p dev/HelloWorld
  • Create a source code directory under the project directory
dev/HelloWorld$ mkdir src
  • Add a Main.cpp file in the source directory src with the following content:
#include <iostream>
int main(){
    
    
	std::cout << "Hello World" << std::endl;
	return 0;
}
  • Create a CMakeLists.txt file in the project root directory and write the compilation process into it
dev/HelloWorld$ touch CMakeLists.txt

The content of CMakeLists.txt is as follows:

cmake_minimum_required (VERSION 3.5)
project (HelloWorld)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")

file (GLOB source_files "${source_dir}/*.cpp")

add_executable (HelloWorld ${
    
    source_files})

Reference:
https://cmake.org/cmake/help/latest/search.html?q=cmake_minimum_required

https://cmake.org/cmake/help/latest/command/project.html#command:project

https://cmake.org/cmake/help/latest/command/set.html?highlight=set

https://cmake.org/cmake/help/latest/command/file.html?highlight=file

https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html

https://cmake.org/cmake/help/latest/search.html?q=PROJECT_SOURCE_DIR

https://cmake.org/cmake/help/latest/command/add_executable.html?highlight=add_executable

Create a build script build.sh in the project root directory

dev/HelloWorld$ touch build.sh

The content of build.sh is as follows:

#!/bin/sh
cmake -G "CodeLite - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug

So far, the situation in the entire HelloWorld directory is as follows:
Please add a picture description

Next we grant executable permissions to the build.sh script:

dev/HelloWorld$ chmod +x build.sh

Execute the build script:

dev/HelloWorld$ ./build.sh

As a result, our directory will look like this:
Please add a picture description
HelloWorld is the executable file compiled this time, let’s execute it:
Please add a picture description

Open the project with CodeLite IDE

Therefore, in the previous work, we have generated all the necessary files, and it is very convenient to use CodeLite to continue working at this time. The method to open a project with CodeLite is as follows:
Please add a picture description
The CodeLite IDE is shown below.
Please add a picture description
With this IDE, of course, it is very comfortable. Of course, on Linux, Qt creator is also a very good choice.

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/128698811