Cmake入门之——Unset方法(七)

一 概述

set方法用于给变量设置值,同理,unset方法用于给变量清空值,其中变量的取值为以下三种:

  • 一般变量(Normal Variable)
  • 缓存变量(Cache Variable)
  • 环境变量(Environment Variable)

二 unset方法说明

2.1 一般变量(Normal Variable)

复制

1
unset(<variable> PARENT_SCOPE)
  • variable:设置要清除的变量名
  • PARENT_SCOPE(可选):如果set方法设置了PARENT_SCOPE,清除时,也请带上PARENT_SCOPE

2.2 缓存变量(Cache Variable)

复制

1
unset(<variable> CACHE)
  • variable:设置要清除的变量名
  • 对于缓存变量清除,带上CACHE

2.3 环境变量(Environment Variable)

复制

1
unset(ENV{<variable>})
  • ENV:Environment的简写,是环境变量的意思
  • variable:设置要清除的变量名

三 示例

3.1 一般变量(Normal Variable)

  • CMakeList.txt中设置

    复制
    1
    2
    3
    4
    5
    
    #normal
    set(normal  "normal" )
    message(WARNING ${normal})
    unset(normal)
    message(WARNING ${normal})
    
  • Debug信息

    复制
    1
    2
    3
    4
    
    CMake Warning at CMakeLists.txt:11 (message):
      normal
    
    CMake Warning at CMakeLists.txt:13 (message):#因为调用了unset,没有信息输出
    

3.2 缓存变量(Cache Variable)

  • CMakeList.txt中设置

    复制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    set(string cmake CACHE STRING "string")#没有多次使用set,导致覆盖variable
    message(WARNING ${string})
    #unset(string)
    unset(string CACHE)
    message(WARNING ${string})
    
    
    
    set(normalCache "normalCache")#多次使用set,导致覆盖variable
    set(normalCache "NewnormalCache" CACHE STRING "string")
    message(WARNING ${normalCache})
    #unset(normalCache)
    unset(normalCache CACHE)
    message(WARNING ${normalCache})
    
  • Debu信息(set方法使用了Cache,清除时unset,也需要使用Cache)

    复制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    CMake Warning at CMakeLists.txt:18 (message):
      cmake
    
    CMake Warning at CMakeLists.txt:21 (message):
    
    CMake Warning at CMakeLists.txt:27 (message):
      NewnormalCache
    
    CMake Warning at CMakeLists.txt:30 (message):
    

3.3 环境变量(Environment Variable)

  • CMakeList.txt中设置

    复制
    1
    2
    3
    4
    5
    
    ##unset(ENV)
    set(ENV{DEFINE} DEFINE) #自定义的变量
    message(WARNING $ENV{DEFINE})
    unset(ENV{DEFINE})
    message(WARNING $ENV{DEFINE})
    
  • Debug信息

    复制
    1
    2
    3
    4
    
    CMake Warning at CMakeLists.txt:35 (message):
      DEFINE
    
    CMake Warning at CMakeLists.txt:37 (message):
    

四 参考

发布了347 篇原创文章 · 获赞 117 · 访问量 52万+

猜你喜欢

转载自blog.csdn.net/Calvin_zhou/article/details/104060935
今日推荐