Linux system using the cmake

Foreword

  Do C programming language common operating systems such as Linux in Ubuntu is: write code, and then use the instructions for program preparation gcc - compiler - Assembler - Links (in fact, only one instruction: gcc -o). Such an approach is only suitable for a single project file no simple file organization. When the project code files and has increased the organizational structure, inevitably need to write makefile. Before you write makefile, scalp tingling, now quickly turned positive, to invest in Cmake.
  Cmake command can be automatically generated according to the project makefile CMakeLists.txt relatively convenient easy to use.

Ready to work

  Cmake installed in Ubuntu:

sudo apt install cmake

  After installation, see cmake version:

cmake --version

Here Insert Picture Description

  Visible current version is 3.5.1, the installation is complete.

Single-file project using cmake

  Create a new folder to store the project, create a new main.c. Internal code as follows:

#include <stdio.h>

int main() {
    printf("hello world!\n");
    return 0;
}

  Clearly a hello world, the program is not important, then a new CMakeLists.txt in the same directory. It reads as follows:

cmake_minimum_required(VERSION 2.8)#cmake最低版本为2.8
project(demo)#工程名
add_executable(main main.c)#生成elf文件名为main,源文件为main.c

  In the absence of file organization, the content is relatively simple. Minimum version of a command is defined cmake 2.8 (the machine cmake version 3.5.1). The second sentence of instructions for the name of the project. The third sentence of instructions executable file name of the project is finally generated main, involved in compiling the source code files for the main.c.
Here Insert Picture Description

  As shown above, the project is ready. Then began compiled executable file. Open a terminal at the project directory. Input:

cmake .

  After entering the execution press Enter. Here we note that there is a directive cmake behind. "" Single dot means "the current directory" . Because of subdirectories when multi-file organization it is two., This will be mentioned later, the first note in advance.
Here Insert Picture Description

  As shown in FIG After executing cmake, generating a number of documents including the makefile contains. In addition to a number of intermediate files generated makefile are cmake.
  Then formally executed from the command line makefile typing make:

make

Here Insert Picture Description
  Visible catalog appeared executable file name for the main. Knock on the command line to execute it:

./main

Here Insert Picture Description

  执行成功了。如果程序有修改,则可以使用命令make clean删除可执行文件,再使用命令make重新编译生成可执行文件。

多文件工程中使用cmake

  一个多文件的工程往往是这样组织的:
Here Insert Picture Description
  文件夹bin:用于存放生成的可执行文件。
  文件夹build:用于存放生成的中间文件。
  文件夹includes:用于存放所有的头文件。
  文件夹sources:用于存放所有的源文件(.c)。
  cmakelists.txt:用于控制全工程的makefile生成。
Here Insert Picture Description
  这里为了测试,在工程中添加两个头文件,三个c文件。存放目录如上图所示。
  func1.c代码如下:

#include "func1.h"
#include <stdio.h>

void DataShow(int data)
{
    printf("the input data is:%d \n",data);//打印一个数字
}

  func1.h代码如下:

#ifndef  _FUNC1_H_
#define _FUNC1_H_
extern void DataShow(int data);
#endif 

  func2.c代码如下:

#include <stdio.h>
#include "func2.h"

void DataAdd(int a,int b)//打印两个数字之和
{
    int c = a+b;
    printf("the number is:%d\n",c);
}

  func2.h代码如下:

#ifndef _FUNC2_H_
#define _FUNC2_H_
extern void DataAdd(int a,int b);

#endif // !_FUNC2_H_

  main.c代码如下:

#include <stdio.h>
#include "func1.h"
#include "func2.h"
int main(void)
{
    DataAdd(2,2);//打印两个数字之和
    printf("data is:%d\n",5);//打印5
    DataShow(5);//打印一个数字
    return 0;
}

  cmakelists.txt内容如下:

cmake_minimum_required(VERSION 2.8)#cmake最低版本为2.8
project(demo)#项目名称
aux_source_directory(sources SRC_LIST)#将源文件夹中的源文件集合定义为变量
include_directories(includes)#添加头文件路径
add_executable(main ${SRC_LIST})#生成elf文件名为main,源文件在源文件夹中
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)#将工程根目录下的bin文件夹作为输出

  命令的前两句不谈了,和之前的一样。
  命令第三句aux_source_directory(sources SRC_LIST)意为将sources文件夹里面的文件集合定义成一个变量,该变量叫SRC_LIST。今后调用这个变量即为调用所有sources文件夹里的源文件。
  命令第四句include_directories(includes)意为将includes文件夹中的头文件添加到工程中。
  命令第五句本质和单文件工程中的一样。生成可执行文件的名称为main,源文件是变量SRC_LIST,即sources文件夹中所有的源文件。这里还需要稍微注意一下,${SRC_LIST}SRC_LIST是个变量,而不是参量,格式需要注意。
  命令第六句意为将输出的可执行文件定位到工程根目录下的bin文件夹。参量EXECUTABLE_OUTPUT_PATH、变量PROJECT_SOURCE_DIR都是cmake中的预设量,分别意为“可执行文件保存的路径”,“工程的根目录”。
  接下来我们需要到build文件夹中执行cmake指令。这样就可以使得生成的中间文件都保存在build文件夹中。
  输入指令:

cmake ..

  由上文可知,由于该工程拥有2级目录因此需要两个点号
Here Insert Picture Description  如上图所示,cmake完毕后生成一个makefile,随后在命令行中进行编译:

make

  注意编译指令也是在build文件夹执行的,因为makefile在build文件夹中。
Here Insert Picture Description

  由上图可见,在bin文件夹下生成了可执行文件main。老办法,执行它。
Here Insert Picture Description
  看到结果,舒服了。

Published 39 original articles · won praise 12 · views 4461

Guess you like

Origin blog.csdn.net/m0_37872216/article/details/103931530