CMAKE 常用指令

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中的变量值)或者空串当变量未定义。
下面看看各选项的意义
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 选项时不能使用(无效)。

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

猜你喜欢

转载自blog.csdn.net/LeGe675797663/article/details/85177052