## CMake and variables

on , , , 2 minutes reading

Yeah, it is me again with more CMake!

You remember my last blog post? When I told you it was the simplest CMake file possible? Well, I lied. In fact, if we just want to print Hello world in CMake we just need to do something like this:

# we just print hello world
message("Hello world!")


It does absolutely nothing except say “Hello world!” when generating the Makefile, and of course, your Makefile will do absolutely nothing.

### Variables

So I have been talking only about CMake Commands. add_executable, link_target_library and message are commands in CMake; they tell CMake what to do and CMake will know how to do it. CMake has a lot of commands and all of them are documented in the CMake documentation.

As in every modern scripting “language”, CMake has variables too. Some of those variables are special and provide special information to CMake, like the directory path where the sources are, the place where we put the compiled executables and libraries, etc. The same as commands, there are a lot of useful variables listed in the CMake wiki and documentation.

#### Setting variables

Setting a variable in CMake is really easy; you just need the command set:

cmake_minimum_required (VERSION 3.5)
project (CMakeVariables)
set (SOURCES hello.c greeter.c)
add_executable (hello ${SOURCES})  As you can see, we need to use the include_directories command to tell CMake where to find our header files. ### File globbing Instead of creating a variable with a explicit list of source files we can “glob” them together using an expression and assign the created list to a variable. This is done with the file command: cmake_minimum_required (VERSION 3.5) project (CMakeVariables) include_directories (${PROJECT_SOURCE_DIR})
file (GLOB SOURCES *.c)