Two methods of compiling C++ code in ubuntu--cmake-g++

As a beginner, according to the content on the blog, when compiling and running the first C++ program, there were many low-level errors. I hope to write them all. I am older and have a bad memory so that I will not forget them later.

Use the system Ubuntu18.04. Both cmake and gcc have been installed. The installation method is not introduced here, but only the problems that follow the online tutorials.

From the collected resources, there are two methods: 1: cmake 2: g++

1.cmake

There is an official tutorial online, in English: please see https://cmake.org/cmake-tutorial/

To use cmake, you must first have a CMakeList.txt file, you need to write the configuration information in this file, and then use cmake to process the file.
The following c++ file will be set, similar to ***.cpp, such as helloworld.cpp

Among them, CMakeList.txt and helloworld.cpp are placed in the Home/wkk/ directory:

To create a new file in Ubuntu, use the touch command; open the file with gedit or vim, and there is also a save command. Type the following in the terminal

 
  1. touch CMakeList.txt

  2. touch helloworld.cpp

Configure the following information in CMakeList.txt;

 
  1. cmake_minimum_required(VERSION 2.8)

  2. #工程名

  3. project(HELLOWORLD)

  4. #包含原程序,即把给定目录下的源程序复制给变量DIR_SRC

  5. #将指定路径下的源文件储存在指定的变量中

  6. #下面这句话,有些博客中写错了,需要注意

  7. aux_source_directory(./ DIR_SRC )

  8. #生成程序

  9. add_executable(HELLOWORLD ${DIR_SRC})

Enter the following information in helloworld.cpp:

 
  1. #include<iostream>

  2. int main()

  3. {

  4. std::cout<<"hello world!"<<std::endl;

  5. return 0;

  6. }

Then type in the terminal:

The build workspace is also under the home/wkk/ directory: that is ~/

 
  1. //按照顺序依次输入下面的命令

  2. // mkdir 做一个工作空间,名称是build

  3. // cd 进入build的目录

  4. //HELLOWORLD 是文件中的工程名称

  5. //以上仅仅是注释

  6.  
  7. mkdir build

  8. cd build

  9. cmake ..

  10. make

  11. ./HELLOWORD

The output is as follows:

 
  1. wkk@wkk-VLT-WX0:~/build$ ./HELLOWORLD

  2. hello world!

  3. wkk@wkk-VLT-WX0:~/build$

In another case, please refer to https://www.cnblogs.com/geooeg/p/7857534.html

All of the CMakeList.txt files and .cpp are in the workspace, so the settings in the CMakeList.txt file are different, and a code that guides the path is omitted.

You can compare the previous one to see what is different. Other runs are the same.

 
  1. #CMakeLists.txt文件内容

  2. #版本号

  3. cmake_minimum_required (VERSION 2.8)

  4. #项目名

  5. project (Tutorial)

  6. #生成可执行程序 语法:add_executable(可执行程序名 要编译的cpp)

  7. add_executable(Tutorial tutorial.cpp)

2. g++

According to the C++ program of helloworld.cpp written above, no need to write #CMakeLists.txt file, you can directly enter the command in the terminal and run

Note that in the program directory of .cpp, run

 
  1. #test 表示的是Project

  2. g++ helloworld.cpp -o test

  3. ./test

Currently the above are all compiled single programs

There is also a way to compile multiple programs:

 

 

The following are commonly used commands for gcc compilation

1. gcc commonly used compilation command options

Assume that the source file is named test.c.

1. Compile and link without option
Usage: #gcc test.c
Function: preprocess, assemble, compile and link test.c to form an executable file. No output file is specified here, and the default output is a.out.

2. Option -o
usage: #gcc test.c -o test
Function: preprocess, assemble, compile and link test.c to form an executable file test. The -o option is used to specify the file name of the output file.

3. Option -E
usage: #gcc -E test.c -o test.i
Function: preprocess test.c and output test.i file.

4. Option -S
usage: #gcc -S test.i 
Function: Assemble the preprocessed output file test.i into a test.s file.

5. Option -c
usage: #gcc -c test.s
Function: compile the assembly output file test.s and output the test.o file.

6. Link without option
Usage: #gcc test.o -o test
Function: Link the compiled output file test.o into the final executable file test.

7. Option -O
usage: #gcc -O1 test.c -o test
Function: Use compiler optimization level 1 to compile the program. The level is 1~3, the larger the level, the better the optimization effect, but the longer the compilation time.

 

2. Compiling method of gcc multi-source file

If there are multiple source files, there are basically two compilation methods:
[Assume that there are two source files test.c and testfun.c]

1. Compile multiple files together
Usage: #gcc testfun.c test.c -o test
Function: Compile testfun.c and test.c separately and link them into test executable files.

2. Compile each source file separately, and then link the object file output after compilation.
Usage:
#gcc -c testfun.c //Compile testfun.c to testfun.o
#gcc -c test.c //Compile test.c to test.o
#gcc -o testfun.o test.o -o test //Link testfun.o and test.o into test

Comparing the above two methods, the first method requires all files to be recompiled when compiling, while the second method can only recompile the modified files, and the unmodified files do not need to be recompiled.

3. If the files to be compiled are all in the same directory, you can use the wildcard gcc *.c -o to compile.

Would you ask, if it’s a project, there may be hundreds of files. With this compilation method, don’t people have to be exhausted in front of the computer, or when you’re compiled successfully, will your hair be gray? So we have to write the above compilation process into the following text file:
Call it makefile under Linux #Here

you can write some file descriptions
MyFirst: MyFirst.o hello.o
g++ MyFirst.o hello.o -o MyFirst
Hello. o:Hello.cpp
g++ -c Hello.cpp -o Hello.o
MyFirst.o:MyFirst.cpp
g++ -c MyFirst.cpp -o MyFirst.o

makefile writing rules:
(1) Behavior comments starting with "#"
( 2) The file dependency is:
    target:components
    rule

Save as MyFirst, type in the terminal: make MyFist

For the use of makefile, please refer to: http://www.cnblogs.com/wang_yb/p/3990952.html

Guess you like

Origin blog.csdn.net/qq_27009517/article/details/112021543