CMake Series Two: Introductory Case - Single Source File

Write a source file

as follows

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 double power(double base,int exponent)
 5 {
 6     int result = base;
 7     int i;
 8     if(exponent ==0){
 9         return 1;
10     }
11     for(i=1;i<exponent;++i){
12         result=result*base;
13     }
14     return result;
15 }
16 
17 int main(int argc,char *argv[])
18 {
19     if(argc<3){
20         printf("Usage:%s base exponent \n",argv[0]);
21         return 1;
22     }
23     double base = atof(argv[1]);
24     int exponent = atoi(argv[2]);
25     double result = power(base,exponent);
26     printf("%g ^ %d is %g\n",base,exponent,result);
27     return 0;
28 }

 

编写CMakeLists.txt

The file is in the same directory as the source file

1  # CMake version requirement 
2 cmake_minimum_required (VERSION 2.8 )
 3  #Project information 
4  project (Demo1)
 5  #Specify build target 
6 add_executable(Demo main.c)

The syntax of CMakeLists.txt is relatively simple, consisting of commands, comments and spaces, where commands are not case-sensitive. The content following the symbol # is considered a comment. A command consists of the command name, parentheses, and parameters, separated by spaces.

For the above CMakeLists.txt file, several commands appear in sequence:

    1. cmake_minimum_required: Specifies the minimum version of CMake required to run this configuration file;
    2. project: The parameter value is Demo1, and the command indicates that the name of the project is Demo1.
    3. add_executable: Compile the source file named main.c into an executable file named Demo.

Compile the project

Now the current project executes "cmake .", gets the Makefile and then uses the make command to compile the Demo1 executable file

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608724&siteId=291194637