【cmake系列使用教程】linux
cmake使用教程(二)-添加库github
cmake使用教程(八)-macro和functionpost
这个系列的文章翻译自官方cmake教程:cmake tutorial。测试
示例程序地址:github.com/rangaofei/t…
不会仅仅停留在官方教程。本人做为一个安卓开发者,实在是没有linux c程序开发经验,望大佬们海涵。教程是在macos下完成,大部分linux我也测试过,有特殊说明的我会标注出来。本教程基于cmake-3.10.2,同时认为你已经安装好cmake。
这个库将包含咱们本身计算一个数字的平方根的计算方法。生成的程序可使用这个库,而不是由编译器提供的标准平方根函数(math.h
)。
在本教程中,咱们将把库放到一个名为mathfunction
的子目录中,在工程目录下新建mathfunction
文件夹。这个文件夹中新建CMakeLists.txt文件,包含如下一行代码:
add_library(MathFunctions mysqrt.cxx)
复制代码
而后在这个文件夹中建立源文件mysqrt.cxx
,它只有一个名为mysqrt
的函数,与编译器的sqrt函数提供了相似的功能。
为了利用新库,咱们在工程根目录下的CMakeLists.txt
中添加add_subdirectory()
来构建咱们本身的库。咱们还添加了另外一个include目录,以便MathFunctions / MathFunctions.h
能够为函数原型找到头文件,该文件代码以下:
double mysqrt(double x);
复制代码
而后建立mysqrt.cxx
文件,内容以下
#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;
}
复制代码
最后一个更改是将新库添加到可执行文件。根目录下CMakeLists.txt的最后添加如下代码
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
复制代码
如今文件目录以下
.
├── CMakeLists.txt
├── MathFunctions
│ ├── CMakeLists.txt
│ ├── MathFunctions.h
│ └── mysqrt.cxx
├── TutorialConfig.h.in
└── tutorial.cxx
复制代码
MathFunctions
是咱们本身构建的库,有时候咱们须要控制这个库是否应该使用,那么能够为使用这个库添加一个开关,在构建大型项目时很是有用。
在项目根目录下的CMakeLists.txt
文件中添加以下代码:
# should we use our own math functions?
option (USE_MYMATH
"Use tutorial provided math implementation" ON)
复制代码
假如你使用的是CMake GUI,USE_MYMATH
默认值是用户能够根据须要更改。该设置将存储在缓存中,以便用户在每次运行CMake时生成默认配置。而后咱们就能够选择性的构建和使用mathfunction库。修改根目录下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})
复制代码
这将使用USE_MYMATH
的设置来肯定是否应该编译和使用mathfunction库。注意,使用一个变量(在本例中是EXTRA_LIBS)来设置可选的库,而后将它们连接到可执行文件中。这是一种常见的方法,用于保持较大的项目具备许多可选组件。 首先在Configure.h.in文件中添加如下内容:
#cmakedefine USE_MYMATH
复制代码
而后咱们就可使用USE_MYMATH
这个变量了,最后修改Tutorial.cxx
源代码以下:
// 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;
}
复制代码
咱们编译执行看如下结果
~/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
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 2
复制代码
~/Desktop/Tutorial/Step2/ ./Tutorial 4
The square root of 4 is 2
复制代码
能够看到,这个开关达到了咱们须要的效果。