(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

 

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