How to use CMake variables

CMake uses many variables that are officially documented. These have default values or values that are based on your project environment. You are allowed to change the value of these variables in your CMakeLists.txt. In addition, you can also define any variables you need. In fact, defining your own custom variables makes your CMakeLists.txt file easy to read and maintain.

To list all the variables that are defined and their current values, try this in a directory that has a CMakeLists.txt file:

$ cmake -LAH

A list of common variables used internally by CMake can be seen here. You can change the value of these variables in your CMakeLists.txt to modify the behavior of CMake for your project. See below for how to set variables.

To set a variable to a single value in your CMakeLists.txt file:

set(JoeLibraries -lfoo)

A variable can be set a list of values:

set(JoeLibraries -lfoo -lbar -lstarwars)

It can also be indented in different ways for ease of reading:

set(
    JoeLibraries
    -lfoo
    -lbar
    -lstarwars
    )

To use a variable in another command:

target_link_libraries(
    helloworld
    ${JoeLibraries}
    )

A variable can also be used to in setting the value of another variable:

set(
    BruceLibraries
    -lkungfu
    ${JoeLibraries}
    )

Strings can be appended naturally to variables:

set(CurDir /home/joe/helloworld)
set(BinDir ${CurDir}/bin)

To check if a variable exists:

if(CurDir)
    message("CurDir variable exists")
endif(CurDir)

To check if a variable does not exist:

if(NOT CurDir)
    message("CurDir variable does not exist")
endif(NOT CurDir)

To check if variable value is equal to a string:

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    message("Build is Debug type")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

Note that the comparison is case-sensitive.

If you want case-insensitive compare, you will need to convert the variable value to lowercase, store it in a new variable and compare that:

STRING(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
    message("Build is debug type")
endif()

Tried with: CMake 2.8.7 and Ubuntu 12.04 LTS

How to view environment variables in Vim

List of all environment variables in Vim.

When Vim is started, it grabs the environment variables of the operating system (like PATH) and it also sets it own internal environment variables (like MYVIMRC).

To see all the environment variables available inside Vim, type :echo $ and press Ctrl-D

To see the value that is currently assigned to a particular environment variable, for example $VIM, type :echo $VIM

For more details go here.

Tried with: Vim 7.3.46 on Windows 7 64-bit

Windows Environment Variables in Vim

All the Windows environment variables are read by Vim and they are available in Vim as Vim environment variables. Vim environment variables begin with the character $. Hence, the Windows environment variable name is prefixed with the $ character when it becomes a Vim environment variable.

For example, the Windows environment variable %USERPROFILE% will be available as $USERPROFILE inside Vim.

This post shows how you can list all the environment variables or view the value of a particular variable.

How to view variables in Vim

To view the list of all variables and their values in Vim:

:let

Note that this does not list environment variables, this post shows how to view those.

If you find it hard to wade through the long list, you can redirect the output of this command to a buffer for further analysis. For example, to load the output of :let into a buffer:

:redir @a
:let
:redir END
"ap

There are many types of variables in Vim and each has a special name or prefix:

g:	global variables
b:	local buffer variables
w:	local window variables
t:	local tab page variables
s:	script-local variables
l:	local function variables
v:	Vim variables.

To list the variables of a particular type, for example global variables, just use its prefix:

:let g:

Tried with: Vim 7.3 and Ubuntu 12.04 LTS