Guidelines and HOWTOs/CMake/FirstProject: Difference between revisions

From KDE Community Wiki
*>Pippin
No edit summary
*>Pippin
No edit summary
Line 5: Line 5:
Let's say you have a very simple C++ application. Create a directory and, within it, create a file called <tt>main.cpp</tt> with the following content:
Let's say you have a very simple C++ application. Create a directory and, within it, create a file called <tt>main.cpp</tt> with the following content:


#include <iostream>
{{Input|1=
int main(int, char**) {
#include <iostream>
    std::cout << "Hello world!" << std::endl;
int main(int, char**) {
    return 0;
    std::cout << "Hello world!" << std::endl;
}
    return 0;
}
}}


Now we need to tell CMake how to build this project. To do this, we will create a file in the same directory called <tt>CMakeLists.txt</tt>. Open this in a text editor.
Now we need to tell CMake how to build this project. To do this, we will create a file in the same directory called <tt>CMakeLists.txt</tt>. Open this in a text editor.
Line 15: Line 17:
First, we tell CMake the minimum version of CMake we require. This not only prints out a helpful error if anyone tries to build with a CMake version that's too old (and may not support features we use), but it also turns off some backwards-compatibility behaviours of CMake.
First, we tell CMake the minimum version of CMake we require. This not only prints out a helpful error if anyone tries to build with a CMake version that's too old (and may not support features we use), but it also turns off some backwards-compatibility behaviours of CMake.


[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required cmake_minimum_required](VERSION 2.8.12)
{{Input|1=
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required cmake_minimum_required](VERSION 2.8.12)
}}


VERSION here is a ''keyword argument''. In this case, it says that the next argument (2.8.12) will be the version number. We're saying we want CMake 2.8.12 because this is still the version shipped by a few Linux distributions. You can set it to, say, 3.0 or 3.1 if you like. Don't forget to look at the correct version of the [http://www.cmake.org/documentation/ CMake documentation] for whatever version you use here.
VERSION here is a ''keyword argument''. In this case, it says that the next argument (2.8.12) will be the version number. We're saying we want CMake 2.8.12 because this is still the version shipped by a few Linux distributions. You can set it to, say, 3.0 or 3.1 if you like. Don't forget to look at the correct version of the [http://www.cmake.org/documentation/ CMake documentation] for whatever version you use here.
Line 21: Line 25:
Now we tell CMake the name of our project. The name is used to name the generated Visual Studio solution file, if you use that build environment, for example.
Now we tell CMake the name of our project. The name is used to name the generated Visual Studio solution file, if you use that build environment, for example.


[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project](FirstProjectTutorial)
{{Input|1=
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project](FirstProjectTutorial)
}}


The [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project] command does more than just name the project; it also sets up the languages the project will use. You can pass the languages you need to the command, but if you don't, C and C++ will be enabled by default.
The [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project] command does more than just name the project; it also sets up the languages the project will use. You can pass the languages you need to the command, but if you don't, C and C++ will be enabled by default.
Line 27: Line 33:
That's our initial setup done. Now we need to tell CMake to make an executable. First, we store a list of source files in a variable:
That's our initial setup done. Now we need to tell CMake to make an executable. First, we store a list of source files in a variable:


[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set](tutorial_SRCS
{{Input|1=
    main.cpp
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set](tutorial_SRCS
)
    main.cpp
)
}}


Variables in CMake behave much like variables in a shell: you don't have to worry about types (they are all strings in the end) and you don't have to declare them before you use them (a variable that hasn't been set is empty). If you wanted to add more source files, you just need to separate them with whitespace (such as one on each line).
Variables in CMake behave much like variables in a shell: you don't have to worry about types (they are all strings in the end) and you don't have to declare them before you use them (a variable that hasn't been set is empty). If you wanted to add more source files, you just need to separate them with whitespace (such as one on each line).
Line 35: Line 43:
Now that we have our list of source files, we can tell CMake to turn them into an executable:
Now that we have our list of source files, we can tell CMake to turn them into an executable:


[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable](tutorial ${tutorial_SRCS})
{{Input|1=
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable](tutorial ${tutorial_SRCS})
}}


This creates an ''executable target'' called <tt>tutorial</tt> (the first argument). A target can be thought of as an "output" of the build system (that's a simplification, but not a bad starting point). If you wanted to create a library, that would be [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_library another type of target]. "Documentation" could be another ([http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_custom_target special kind of]) target. Everything in CMake is aimed at describing what targets you want your build system to produce and how to make (and install) them.
This creates an ''executable target'' called <tt>tutorial</tt> (the first argument). A target can be thought of as an "output" of the build system (that's a simplification, but not a bad starting point). If you wanted to create a library, that would be [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_library another type of target]. "Documentation" could be another ([http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_custom_target special kind of]) target. Everything in CMake is aimed at describing what targets you want your build system to produce and how to make (and install) them.
Line 45: Line 55:
Putting it all together, your file should look like:
Putting it all together, your file should look like:


[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required cmake_minimum_required](VERSION 2.8.12)
{{Input|1=
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project](FirstProjectTutorial)
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required cmake_minimum_required](VERSION 2.8.12)
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set](tutorial_SRCS
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:project project](FirstProjectTutorial)
    main.cpp
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set](tutorial_SRCS
)
    main.cpp
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable](tutorial ${tutorial_SRCS})
)
[http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable](tutorial ${tutorial_SRCS})
}}


Now you need to run CMake on it. [[Guidelines HOWTOs/CMake/Building]] has all the details, but very briefly, on a Unix system you can run
Now you need to run CMake on it. [[Guidelines HOWTOs/CMake/Building]] has all the details, but very briefly, on a Unix system you can run


mkdir build
{{Input|1=
cd build
mkdir build
cmake ..
cd build
make
cmake ..
make
}}


from the source directory, and on other systems you can use the CMake GUI (just point it at your source directory and create an empty directory for the build directory, then press Configure followed by Generate) to create project files for your favourite build environment (such as XCode), and use that to do the compilation.
from the source directory, and on other systems you can use the CMake GUI (just point it at your source directory and create an empty directory for the build directory, then press Configure followed by Generate) to create project files for your favourite build environment (such as XCode), and use that to do the compilation.
Line 63: Line 77:
You can run the resulting executable, and get the output you would expect from any first programming-related tutorial:
You can run the resulting executable, and get the output you would expect from any first programming-related tutorial:


Hello world!
{{Output|1=
Hello world!
}}

Revision as of 11:06, 28 July 2015

This tutorial will show you how to create a very simple project in CMake.

First, you need to make sure you have CMake installed. If you are using Linux, you should be able to get CMake from your distribution's packages. On Windows or OS/X, you can install the packages provided by CMake.

Let's say you have a very simple C++ application. Create a directory and, within it, create a file called main.cpp with the following content:

#include <iostream>
int main(int, char**) {
    std::cout << "Hello world!" << std::endl;
    return 0;
}

Now we need to tell CMake how to build this project. To do this, we will create a file in the same directory called CMakeLists.txt. Open this in a text editor.

First, we tell CMake the minimum version of CMake we require. This not only prints out a helpful error if anyone tries to build with a CMake version that's too old (and may not support features we use), but it also turns off some backwards-compatibility behaviours of CMake.

cmake_minimum_required(VERSION 2.8.12)

VERSION here is a keyword argument. In this case, it says that the next argument (2.8.12) will be the version number. We're saying we want CMake 2.8.12 because this is still the version shipped by a few Linux distributions. You can set it to, say, 3.0 or 3.1 if you like. Don't forget to look at the correct version of the CMake documentation for whatever version you use here.

Now we tell CMake the name of our project. The name is used to name the generated Visual Studio solution file, if you use that build environment, for example.

project(FirstProjectTutorial)

The project command does more than just name the project; it also sets up the languages the project will use. You can pass the languages you need to the command, but if you don't, C and C++ will be enabled by default.

That's our initial setup done. Now we need to tell CMake to make an executable. First, we store a list of source files in a variable:

set(tutorial_SRCS
    main.cpp
)

Variables in CMake behave much like variables in a shell: you don't have to worry about types (they are all strings in the end) and you don't have to declare them before you use them (a variable that hasn't been set is empty). If you wanted to add more source files, you just need to separate them with whitespace (such as one on each line).

Now that we have our list of source files, we can tell CMake to turn them into an executable:

add_executable(tutorial ${tutorial_SRCS})

This creates an executable target called tutorial (the first argument). A target can be thought of as an "output" of the build system (that's a simplification, but not a bad starting point). If you wanted to create a library, that would be another type of target. "Documentation" could be another (special kind of) target. Everything in CMake is aimed at describing what targets you want your build system to produce and how to make (and install) them.

After the target name comes the list of sources. This demonstrates how we reference variables - again, much like a shell. If we had provided multiple source files to the set command, this would expand to multiple arguments passed to add_executable.

And that's it! In a handful of lines, we have a project that will allow us to build an executable across multiple compilers and platforms. It won't install anything yet, although that would be a simple addition.

Putting it all together, your file should look like:

cmake_minimum_required(VERSION 2.8.12)
project(FirstProjectTutorial)
set(tutorial_SRCS
    main.cpp
)
add_executable(tutorial ${tutorial_SRCS})

Now you need to run CMake on it. Guidelines HOWTOs/CMake/Building has all the details, but very briefly, on a Unix system you can run

mkdir build
cd build
cmake ..
make

from the source directory, and on other systems you can use the CMake GUI (just point it at your source directory and create an empty directory for the build directory, then press Configure followed by Generate) to create project files for your favourite build environment (such as XCode), and use that to do the compilation.

You can run the resulting executable, and get the output you would expect from any first programming-related tutorial:

Hello world!