KDevelop4/Manual/Getting started: A small CMake tutorial
In this chapter, you will learn how to build your CMake based project and how to add files.
If you want do get a more in-depth tutorial on CMake, see Appendix B: CMake for bigger projects.
Adding a file
Right-click on the project's main target and select helloworld.cpp
here and click on "OK".
The dialog that pops up now shows the changes that will be made to CMakeLists.txt
. For now, click on "OK".
The newly created file is now opened in the editor. Enter the following code and save the file:
#include <iostream> void heyWorld() { std::cout << "Hello, world!"; }
First compilation
There are several ways to compile your program.
- Target context menu
- Right-click on the target you want to build and click on .
- Projects Toolview button
- Click on the small button with the gear on it – it has the same functionality as the menu entry in the target context menu.
- Click on F8. or press
- This is still the same functionality as described above.
- Click on .
- This does what you expect – it builds all your projects.
Now simply press F8.
On the first pass, KDevelop calls CMake and prints its output to the Build Toolview which pops up at the bottom:
/home/sto/projects/kdevbook/build> /usr/bin/cmake ‐DCMAKE_BUILD_TYPE=Debug /home/sto/projects/kdevbook/ ‐‐ The C compiler identification is GNU ‐‐ The CXX compiler identification is GNU ‐‐ Check for working C compiler: /usr/bin/gcc ‐‐ Check for working C compiler: /usr/bin/gcc ‐‐ works ‐‐ Detecting C compiler ABI info ‐‐ Detecting C compiler ABI info ‐ done ‐‐ Check for working CXX compiler: /usr/bin/c++ ‐‐ Check for working CXX compiler: /usr/bin/c++ ‐‐ works ‐‐ Detecting CXX compiler ABI info ‐‐ Detecting CXX compiler ABI info ‐ done ‐‐ Configuring done ‐‐ Generating done ‐‐ Build files have been written to: /home/sto/projects/kdevbook/build
After this step, the Makefiles have been generated, and KDevelop calls make
:
/home/sto/projects/kdevbook/build> make Scanning dependencies of target kdevbook [ 50%] Building CXX object CMakeFiles/kdevbook.dir/helloworld.cpp.o [100%] Building CXX object CMakeFiles/kdevbook.dir/main.cpp.o Linking CXX executable kdevbook [100%] Built target kdevbook *** Finished ***
Congratulations, you built your first application using KDevelop!