KDevelop4/Manual/Getting started: A small CMake tutorial: Difference between revisions

From KDE UserBase Wiki
No edit summary
No edit summary
Line 58: Line 58:


Congratulations, you built your first application using KDevelop!
Congratulations, you built your first application using KDevelop!
== More about targets ==
Now add the header file <code>helloworld.h</code> with the following contents to your project:
{{Input|1=void helloWorld;}}
And replace the <code>#include</code> directive with the following line at the top of <code>main.cpp</code>:
{{Input|1=#include "helloworld.h"}}
Finally, replace the line containing <code>std::cout << ...</code> with a call to <code>heyWorld();</code>. Now build your project again.
Let's take a look at <code>CMakeLists.txt</code> &ndash; open it by double-clicking on the file in the Projects Toolview. The first line in it defines the project's name. You may define the languages used by the project here &ndash; if not defined, they default to C and C++. Take a look at [http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:project the official documentation for the <code>project</code> command].


{{KDevelop4Nav|prev=Getting started|next=Getting started: Sessions and Projects}}
{{KDevelop4Nav|prev=Getting started|next=Getting started: Sessions and Projects}}

Revision as of 08:26, 8 December 2010

Under Construction

This is a new page, currently under construction!


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

Create a File in a Target

Right-click on the project's main target and select Create File. You will be asked for a filename – enter 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 Build.
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 Project -> Build Selection or press F8.
This is still the same functionality as described above.
Click on Project -> Build all Projects.
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!

More about targets

Now add the header file helloworld.h with the following contents to your project:

void helloWorld;

And replace the #include directive with the following line at the top of main.cpp:

#include "helloworld.h"

Finally, replace the line containing std::cout << ... with a call to heyWorld();. Now build your project again.

Let's take a look at CMakeLists.txt – open it by double-clicking on the file in the Projects Toolview. The first line in it defines the project's name. You may define the languages used by the project here – if not defined, they default to C and C++. Take a look at the official documentation for the project command.