cmake的兩個命令: option 和 configure_file

原文鏈接:https://www.cnblogs.com/the-capricornus/p/4717566.html

本節要討論的是cmake的兩個命令: option 和 configure_file
option 選項,讓你可以根據選項值進行條件編譯。
configure_file 配置文件,讓你可以在代碼文件中使用CMake中定義的的變量

option
Provides an option that the user can optionally select.
option 提供一個用戶可以任選的選項。語法如下

option(<option_variable> "help string describing option"  [initial value])

Provide an option for the user to select as ON or OFF. If no initial value is provided, OFF is used.
option 提供選項讓用戶選擇是 ON 或者 OFF ,如果沒有提供初始化值,使用OFF。
也就是說默認的值是OFF。

但是請注意:這個option命令和你本地是否存在編譯緩存的關係很大。
所以,如果你有關於 option 的改變,那麼請你務必清理 CMakeCache.txt 和 CMakeFiles 文件夾。
還有,請使用標準的 [initial value] 值 ON 或者 OFF。

可以在命令行通過以下的方式設置選項
比如想打開 FOO_ENABLE 選項 -DFOO_ENABLE=ON

configure_file
configure_file 的作用是讓普通文件也能使用CMake中的變量。
也就是說代碼文件中可以使用CMake中的變量。
語法如下:
configure_file(<input> <output>
                    [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
                    [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
Copies an <input> file to an <output> file and substitutes variable values referenced as @VAR@ or ${VAR} in the input file content.
Each variable reference will be replaced with the current value of the variable, or the empty string if the variable is not defined.
Furthermore, input lines of the form:

拷貝一個 <input>(輸入文件) 文件到 <output> (輸出文件),並且替換輸入文件中被 @VAR@ 或者 ${VAR} 引用的變量值。每一個變量將被替換成當前的變量值(注:CMake中的變量值)或者空串當變量未定義。

輸入文件中的
#cmakedefine VAR ...

將被替換爲
#define VAR ...

或者

/* #undef VAR */
下面看看各選項的意義
COPYONLY
    Copy the file without replacing any variable references or other content. This option may not be used with NEWLINE_STYLE.
    只拷貝文件,不進行任何的變量替換。這個選項在指定了 NEWLINE_STYLE 選項時不能使用(無效)。
ESCAPE_QUOTES
    Escape any substituted quotes with backslashes (C-style).
    躲過任何的反斜槓(C風格)轉義。
    注:躲避轉義,比如你有個變量在CMake中是這樣的
    set(FOO_STRING "\"foo\"")
    那麼在沒有 ESCAPE_QUOTES 選項的狀態下,通過變量替換將變爲 ""foo"",如果指定了 ESCAPE_QUOTES 選項,變量將不變。
@ONLY
    Restrict variable replacement to references of the form @VAR@. This is useful for configuring scripts that use ${VAR} syntax.
    限制變量替換,讓其只替換被 @VAR@ 引用的變量(那麼 ${VAR} 格式的變量將不會被替換)。這在配置 ${VAR} 語法的腳本時是非常有用的。
    
NEWLINE_STYLE <style>
    Specify the newline style for the output file. Specify UNIX or LF for \n newlines, or specify DOS, WIN32, or CRLF for \r\n newlines. This option may not be used with COPYONLY.
    指定輸出文件中的新行格式。UNIX 和 LF 的新行是 \n ,DOS 和 WIN32 和 CRLF 的新行格式是 \r\n 。 這個選項在指定了 COPYONLY 選項時不能使用(無效)。
    
Example

Consider a source tree containing a foo.h.in file:

#cmakedefine FOO_ENABLE
#cmakedefine FOO_STRING "@FOO_STRING@"

An adjacent CMakeLists.txt may use configure_file to configure the header:

option(FOO_ENABLE "Enable Foo" ON)
if(FOO_ENABLE)
  set(FOO_STRING "foo")
endif()
configure_file(foo.h.in foo.h @ONLY)

This creates a foo.h in the build directory corresponding to this source directory. If the FOO_ENABLE option is on, the configured file will contain:

#define FOO_ENABLE
#define FOO_STRING "foo"

Otherwise it will contain:

/* #undef FOO_ENABLE */
/* #undef FOO_STRING */

One may then use the include_directories() command to specify the output directory as an include directory:

include_directories(${CMAKE_CURRENT_BINARY_DIR})

so that sources may include the header as #include <foo.h>.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章