CMake记录(二)

Wiki, 中列出了可利用的主要资源、与开发相关的话题、一些教程及如何从其他构建系统转移到CMake。

 Documentation,页中也列出了一些资源

CMake Tutorial节选自Master CMake一书,由简入繁,是个好教程。

这个教程共七步,后面两步是讲CPack和CTest的,暂时不理,我将前五步的代码一并发上来。这里

CPack 可以看这里:Packaging Software with CPack

CTest及DART : Testing Software with CTest and DART

–> Examples

不是很详细,但相信看过上面的教程这个就是小菜了。

我们已经写过一些CMakeLists了,但CMake的语法还是有必要系统认识一下,详细地可以看这里,这里简要叙述一下:

命令:

command (args...) args间用 white space(spaces, line feeds ,tabs)格开

变量:

set(VAR a;b;c)     set(VAR a b c) 这两种形式等价

其中基本数据类型是string,变量用${VAR}引用

若有 set (Foo a b c)

则 command(${Foo}) 与 command(a b c)等价

如果想把一串变量作了一个变量进行传递,则用双引号包围起来:

command(“${Foo}”) 相当于 command(“a b c”)

控制结构:

  • conditional statements: if

    # some_command will be called if the variable's value is not:
    # empty, 0, N, NO, OFF, FALSE, NOTFOUND, or -NOTFOUND.
    if(var)
       some_command(...)
    endif(var)

  • looping constructs: foreach and while

    set(VAR a b c)
      # loop over a, b,c with the variable f
    foreach(f ${VAR})
        message(${f})
    endforeach(f)

  • 过程定义:宏和函数. functions create a local scope for variables, and macros use the global scope.

    # define a macro hello
    macro(hello MESSAGE)
        message(${MESSAGE})
    endmacro(hello)
    # call the macro with the string "hello world"
    hello("hello world")
    # define a function hello
    function(hello MESSAGE)
        message(${MESSAGE})
    endfunction(hello)

引号, 字符串 and 转义

将字符串双引号包围得到其字面值,其中可包含换行,如:

set (MY_STRING "this is a string with a
  newline in
  it")

可对其中的特殊字符转义:

set (VAR "
   hello
  world
  ")
message ( "\${VAR} = ${VAR}")
  # prints out
  ${VAR} =
    hello
    world

也支持类C的标准转义:

message("\n\thello world")
# prints out
hello world

正则表达式:

有一些CMake命令,如if and string使用正则表达式,或可使用正则作参数

    • ^ Matches at beginning of a line or string
    • $ Matches at end of a line or string
    • . Matches any single character other than a newline
    • [ ] Matches any character(s) inside the brackets
    • [^ ] Matches any character(s) not inside the brackets
    • [-] Matches any character in range on either side of a dash
    • * Matches preceding pattern zero or more times
    • + Matches preceding pattern one or more times
    • ? Matches preceding pattern zero or once only
    • () Saves a matched expression and uses it in a later replacement

如果这些内容都看过了,不妨还是浏览官方文档,列出非常清楚,遇到问题时到这里查找一下也基本能解决。

原文地址:https://www.cnblogs.com/justin_s/p/2235177.html