Guidelines and HOWTOs/CMake/FirstProject: Difference between revisions
*>Pippin Created page with "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..." |
No edit summary |
||
(11 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
This tutorial will show you how to create a very simple project in CMake. | This tutorial will show you how to create a very simple project in CMake. | ||
{{Note|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 [http://www.cmake.org/download/ packages provided by CMake].}} | |||
==Tutorial== | |||
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: | ||
{{Input|1=<syntaxhighlight lang="cpp"> | |||
#include <iostream> | |||
int main(int, char**) { | |||
std::cout << "Hello world!" << std::endl; | |||
return 0; | |||
} | |||
</syntaxhighlight>}} | |||
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. | ||
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 | 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. | ||
{{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] | 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 (so we don't want to set it to a later version), but 2.8.12 has useful features we will want in later tutorials (so we don't set it to an earlier version). 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. | ||
Now we tell CMake the name of our project. | Now we tell CMake the name of our project. This is used to name the generated Visual Studio solution file, if you use that build environment, for example. | ||
{{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 35: | ||
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: | ||
{{Input|1= | |||
[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 45: | ||
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: | ||
{{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 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. | 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. | ||
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 [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set] command, this would expand to multiple arguments passed to [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable]. | 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 [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:set set] command, this would expand to multiple arguments passed to [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable]. | ||
We could have skipped the variable altogether, and just listed the sources directly in the [http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable add_executable] command. Having a separate variable is a commonly-used pattern, however, as it makes it easy to have, say, different source files on different platforms. | |||
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. | 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. | ||
==Build== | |||
Putting it all together, your file should look like: | Putting it all together, your file should look like: | ||
{{Input|1= | |||
[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:project project](FirstProjectTutorial) | |||
[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}) | |||
}} | |||
Now you need to run CMake on it. | Now you need to run CMake on it. [[Guidelines and HOWTOs/CMake/Building]] has all the details, but very briefly, on a Unix system you can run | ||
{{Input|1= | |||
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. | 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 83: | ||
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: | ||
{{Output|1= | |||
Hello world! | |||
}} | |||
==Next steps== | |||
Try adding another source file to the project (this will require some C++ knowledge if you want your second source file to actually be used). It should be as simple as adding the name of the new file next to <tt>main.cpp</tt> (on a new line, for readability). |
Latest revision as of 16:29, 10 March 2016
This tutorial will show you how to create a very simple project in CMake.
Tutorial
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 (so we don't want to set it to a later version), but 2.8.12 has useful features we will want in later tutorials (so we don't set it to an earlier version). 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. This 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.
We could have skipped the variable altogether, and just listed the sources directly in the add_executable command. Having a separate variable is a commonly-used pattern, however, as it makes it easy to have, say, different source files on different platforms.
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.
Build
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 and 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!
Next steps
Try adding another source file to the project (this will require some C++ knowledge if you want your second source file to actually be used). It should be as simple as adding the name of the new file next to main.cpp (on a new line, for readability).