Cmake (cmake)

Cmake Use tutorial (1) – start

Cmake Tutorial 2 – Adding libraries

Cmake use tutorial (3) – installation, testing, system self – check

Cmake Use tutorial (4) – file generator

Cmake Use tutorial (5) -cpack generate installation package

Cmake Use tutorial (6) – lousy syntax

Cmake Use Tutorial (7) – Processes and loops

Cmake: Macro and Function

This series of articles was translated from the official Cmake tutorial: CMake Tutorial.

Example program address: github.com/rangaofei/t…

It will not stop at the official tutorial. I as an Android developer, is really no Linux C program development experience, hope big guys forgive. The tutorial is done on MacOS, and I’ve tested most Linux as well, so IF there are special instructions, I’ll note them. This tutorial is based on cmake-3.10.2 and assumes that you have cmake installed.

Build your own library

This library will contain our own methods for calculating the square root of a number. The generated program can use this library instead of the standard square root function (math.h) provided by the compiler.

In this tutorial, we’ll put the library in a subdirectory called MathFunction and create a new mathFunction folder under the project directory. In this folder, create a new cMakelists.txt file with the following line of code:

add_library(MathFunctions mysqrt.cxx)
Copy the code

Then create the source file mysqrt. CXX in this folder, which has only one function named mysqrt, which provides similar functionality to the compiler’s SQRT function.

To take advantage of the new library, we added add_subdirectory() to cmakelists.txt in the project root directory to build our own library. We also added another include directory so that MathFunctions/mathfunctions.h can find the header file for the function prototype, which has the following code:

double mysqrt(double x);
Copy the code

Then create the mysqrt.cxx file with the following content

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

// a hack square root calculation using simple operations
double mysqrt(double x)
{
  if (x <= 0) {
    return 0;
  }

  double result;
  double delta;
  result = x;

  // do ten iterations
  int i;
  for (i = 0; i < 10; ++i) {
    if(result <= 0) {result = 0.1; } delta = x - (result * result); Result = result + 0.5 * delta/result; fprintf(stdout,"Computing sqrt of %g to be %g\n", x, result);
  }
  return result;
}
Copy the code

The final change is to add the new library to the executable. Add the following code at the end of cMakelists.txt in the root directory

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
Copy the code

Now the file directory is as follows

.├ ─ mathfunctions.txt │ ├─ mathfunctions.h │ ├─ mathfunctions.h │ ├─ mysqr.cxx ├─ TutorialConfig. H.i n └ ─ ─ tutorial. CXXCopy the code

Build optional

MathFunctions is a library we built ourselves, and sometimes we need to control whether or not the library should be used, so adding a switch to use the library can be very useful when building large projects.

Add the following code to the cMakelists. TXT file in the project root directory:

# should we use our own math functions?
option (USE_MYMATH 
        "Use tutorial provided math implementation" ON)
Copy the code

If you are using CMake GUI, the default value of USE_MYMATH is user-configurable. This setting is stored in the cache so that the user generates the default configuration each time CMake is run. We can then selectively build and use mathFunction libraries. Modify root cmakelists. TXT:

# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})
Copy the code

This will use the setting of USE_MYMATH to determine whether the MathFunction library should be compiled and used. Note that you use a variable (in this case, EXTRA_LIBS) to set up the optional libraries, and then link them into the executable. This is a common approach for keeping large projects with many optional components. First add the following to the configure.h.id file:

#cmakedefine USE_MYMATH
Copy the code

Then we can use the USE_MYMATH variable and finally modify the tutorial. CXX source code as follows:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d\n", argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
    }
 
  double inputValue = atof(argv[1]);
 
#ifdef USE_MYMATH
  double outputValue = mysqrt(inputValue);
#else
  double outputValue = sqrt(inputValue);
#endif
 
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}
Copy the code

Let’s compile the execution to see the following results

  1. Use a custom library (USE_MYMATH=ON)
 ~/Desktop/Tutorial/Step2/./Tutorial 4 Computing SQRT of 4 to be 2.5 Computing SQRT of 4 to be 2.05 Computing SQRT of 4 to be 2.00061 Computing SQRT of 4 to be 2 Computing SQRT of 4 to be 2 Computing SQRT of 4 to be 2 Computing SQRT of 4 to be 2 to be 2 Computing sqrt of 4 to be 2 Computing sqrt of 4 to be 2 Computing sqrt of 4 to be 2 The square root of 4 is 2Copy the code
  1. Custom libraries not applicable (USE_MYMATH=OFF)
 ~/Desktop/Tutorial/Step2/./Tutorial 4 The square root of 4 is 2Copy the code

As you can see, this switch does exactly what we want.