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:


To use a variable in another command:


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


Strings can be appended naturally to variables:

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

To check if a variable exists:

    message("CurDir variable exists")

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:

    message("Build is Debug type")

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:

    message("Build is debug type")

Tried with: CMake 2.8.7 and Ubuntu 12.04 LTS

One thought on “How to use CMake variables

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.