(4)CMake入门笔记--CMake官网教程

四.添加系统内省

接下来让我们考虑在项目中添加一些代码,这些代码取决于目标平台可能没有的功能。例如是否具有log和exp函数。 当然,几乎每个平台都有这些功能,但本教程假设它们不太常见。 如果平台有日志,那么我们将使用它来计算mysqrt函数中的平方根。 我们首先使用顶级CMakeLists.txt文件中的CheckFunctionExists.cmake宏测试这些函数的可用性,如下所示:

# does this system provide the log and exp functions?
include (CheckFunctionExists)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

接下来我们修改TutorialConfig.h.in以定义这些值,如果CMake在平台上找到它们,如下所示:

// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

在TutorialConfig.h的configure_file命令之前完成log和exp的测试非常重要。 configure_file命令使用CMake中的当前设置立即配置文件。 最后在mysqrt函数中,如果使用以下代码在系统上可用,我们可以提供基于log和exp的备用实现:

// if we have both log and exp then use them
#if defined (HAVE_LOG) && defined (HAVE_EXP)
  result = exp(log(x)*0.5);
#else // otherwise use an iterative approach

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章