MRC/Tutorials/Setting up an IDE: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
(Created page with '= Introduction = To keep the code in your packages clear and manageable, it is advised to use an ''Integrated Development Environment'' (IDE) to edit your C++ code. Qt Creator i…')
 
 
(18 intermediate revisions by 6 users not shown)
Line 1: Line 1:
= Introduction =
= Introduction =


To keep the code in your packages clear and manageable, it is advised to use an ''Integrated Development Environment'' (IDE) to edit your C++ code. Qt Creator is an IDE for C++. It has the advantage of understanding you code up to some extend. This means Qt Creator can be used to, e.g., auto-complete names of variables and functions or get compilation error message in a nice way.
To keep the code in your packages clear and manageable, it is advised to use an ''Integrated Development Environment'' (IDE) to edit your C++ code. [http://qt-project.org/wiki/category:tools::qtcreator Qt Creator] is such an IDE for C++. It has the advantage of understanding your code up to some extent. This means Qt Creator can be used to, e.g., auto-complete names of variables and functions or get compilation error message in a nice way. Also, as was stated in the previous tutorial, it understands ''CMake'', which allows it do be used to compile your project, and even run the resulting executables.


== Installing Qt Creator ==
= Installing Qt Creator =


'''Don't use the software center or apt-get to install Qt, then all this will not work properly. Just follow the steps below.'''
# Download Qt Creator for Linux:
<pre>
sudo apt-get install qtcreator
</pre>
<!--
#* [http://download.qt.io/official_releases/qtcreator/3.3/3.3.2/qt-creator-opensource-linux-x86-3.3.2.run 32-bit]
#* [http://download.qt.io/official_releases/qtcreator/3.3/3.3.2/qt-creator-opensource-linux-x86_64-3.3.2.run 64-bit]


# Download Qt Creator for Linux:
#* [http://download.qt-project.org/archive/qtcreator/2.5/qt-creator-linux-x86-opensource-2.5.2.bin  32-bit]
#* [http://download.qt-project.org/archive/qtcreator/2.5/qt-creator-linux-x86_64-opensource-2.5.2.bin 64-bit]
# Open a terminal, ''cd'' to the folder where you downloaded the file (probably ''~/Downloads'')
# Open a terminal, ''cd'' to the folder where you downloaded the file (probably ''~/Downloads'')
# Make the file executable: <pre>chmod +x <INSTALL_FILE></pre>
# Make the file executable: <pre>chmod +x <INSTALL_FILE></pre>
# Run the installation: <pre>./<INSTALL_FILE></pre>
# Run the installation: <pre>./<INSTALL_FILE></pre>
# Make sure Qt Creator launches with the ROS environment available:
-->
## Open the launcher properties (right-click Applications -> Edit Menus -> Programming -> Qt Creator -> Properties
If you installed Qt Creator in the default path, you can run it by calling:
## In the 'Command' field, add ''bash -i -c '' in front of the existing command.
<pre>
 
qtcreator
Now you can correctly launch Qt Creator from the menu.
</pre>
 
<!--
== Use with ROS ==
<pre>
 
~/qtcreator-3.3.2/bin/qtcreator
=== Creating a ROS package ===
 
To create a ROS package, follow the usual steps, ''i.e.'', use the ''roscreate-pkg'' command and add the package to the SVN.
 
=== Open an existing ROS package ===
 
# Make sure your package is in the ROS_PACKAGE_PATH, you can check this by navigating to your package using:
<pre> roscd <your_package_name>
</pre>
</pre>
# Start Qt Creator
-->
# File -> Open File or Project
# Navigate to and open the '''CMakeLists.txt''' file of your package
# Change the build directory into ''<YOUR_PACKAGE_DIR>/build'' (that is: a '''/''' instead of a '''-'''). Click 'Next'.
# Click 'Run CMake'. This will automatically look for and include dependencies in the '''manifest.xml''' file, see which ''.cpp'' files you use, etc, based on your CMakeLists.txt.
# Click 'Finish'


Now you are ready to start developing your code. Pressing ''Ctrl-b'' will build the active project. If you change the '''manifest.xml''' or '''CMakeLists.txt''' file, you only have to run CMake again (''Build -> Run CMake''). After compiling your package, Qt will show the warnings and errors in a much more user friendly way then your terminal!


=== Show header files in the 'Projects View' ===


Qt Creator only shows your ''.cpp'' files in the 'Project View', which can be a bit of a nuisance. Often you would also like to see header files (.h) if you have them. To also show your header files, perform the following trick:
Caution: if you do not run Qt Creator from the terminal, the path will not be set correctly and the building of your project will fail!


# Open ''CMakeLists.txt''
= Use Qt Creator for your project =
# Add the following line before ''rosbuild_add_executable'': <pre>file(GLOB_RECURSE HEADER_FILES include/*.h)</pre>This looks for all ''.h'' files in the ''include'' folder of your package, and stores them in the variable ''HEADER_FILES''.
# Add ''${HEADER_FILES}'' to your ''rosbuid_add_executable'' list. For example:<pre>rosbuild_add_executable(my_program src/program.cpp ${HEADER_FILES})</pre>
# Within Qt Creator, run CMake (''Build -> Run CMake'')


You should now be able to see all ''.h'' files in the ''include'' folder of your package.
Now you have installed a proper IDE, you can start to do some real programming! In previous tutorials, we created a C++ project called ''my_project'' and went through a little bit of work to get it to build using CMake. Now, that work will pay off: Qt Creator 'understands' CMake, so we can directly load the project. Simply start Qt Creator and:


=== Troubleshoot ===


==== CMake Error ====
# From the home screen (''Welcome'' tab on the left) select ''Open Project''.
# Navigate to the CMakeLists.txt in your project and open it.
# Cick on "Details" for the Desktop setting and leave only the box "Default" checked. Click "Browse..." and select the build folder that you want the cmake output to be built in (e.g. myproject/build as in the previous tutorial)
# Hit the "Configure Project" button, you should now be able to compile and run using the green "Play" button in the left bottom.
# If QtCreator complains about a missing executable file, closing it and reopening from the terminal will likely fix this (it is a known isssue).


If you try to open your project in Qt Creator and you get the following message when running CMake:
<!--
# Now, Qt Creator will pop-up a window titled ''Build location''. Remember last tutorial? Qt Creator wants to create a ''build'' directory in which it will store the build files CMake generates. We already created a ''build'' directory (in the root of your project), so we can simply point Qt Creator to this directory. If you removed it for whatever reason, you can simply tell Qt Creator to create a new directory called ''build''.
# Next, a screen titled ''Run CMake'' pops up. Hit the ''Run CMake'' button. This will literally run CMake, the same way we did before from the command-line (''cmake ..''). After this, press ''Finish'', and you're done!
-->


<pre>
Qt Creator may seem daunting at first: there is a lot that you can do with it. However, we will just use some basic things, so don't worry. On the left, you see a menu with ''Welcome'', ''Edit'', ''Design'', etc. You will mostly be using ''Edit''. To the right of that, there is a window showing your project layout. You can see the name of your project, as specified in the ''CMakeLists.txt'', you can see the ''CMakeLists.txt'' itself, and the ''src'' directory with ''example.cpp'' inside. From this point on you don't really have to leave Qt Creator any more. You can edit the source file in Qt Creator, edit the CMakeLists.txt, run CMake by right clicking the project name and selecting ''Run CMake'', compile the project using ''ctrl-B'' and even run your program using the green 'play' button in the lower left. That's pretty awesome!
CMake Error: The current CMakeCache.txt directory <YOUR_PACKAGE>/build/CMakeCache.txt is different
than the directory <OTHER_DIR>/build where CMakeCache.txt was created. This may result in binaries
being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
</pre>


you have probably moved your package without cleaning it (''make clean''). This means the absolute paths that where generated during building do not make sense anymore. To solve this, do the following:
But you ain't seen nothing yet. Double click on ''example.cpp'' to edit it. Remove the ''#include'' statement at the top, and type it again, but slowly. You will see that as soon as you type the ''#'', Qt Creator pops up a window with some suggestions. The more you type, the more specific it becomes. You can select an option using the arrow keys, and press enter or tab to confirm. This is called ''auto-completion'' and will save you a lot of typing. Continue typing. When you get to ''<em'', you will see that Qt Creator even understands the location of header files, and auto-completes them for you.


* Clean your project, ''i.e.'', type ''make clean''
Now lets make a mistake on purpose: misspell ''return'' and type ''ctrl-B'' to compile the project. You will see a list of issues pop up on the bottom, and a red dot besides the line that contains the error. You can double click on issues to directly jump to the error, which is especially useful if your project gets bigger.
* Make sure there is no ''CmakeLists.txt.user'' in your package (it is auto-generated by Qt Creator and should '''not'''be on the SVN)
* Try to open your project again

Latest revision as of 08:04, 6 May 2021

Introduction

To keep the code in your packages clear and manageable, it is advised to use an Integrated Development Environment (IDE) to edit your C++ code. Qt Creator is such an IDE for C++. It has the advantage of understanding your code up to some extent. This means Qt Creator can be used to, e.g., auto-complete names of variables and functions or get compilation error message in a nice way. Also, as was stated in the previous tutorial, it understands CMake, which allows it do be used to compile your project, and even run the resulting executables.

Installing Qt Creator

  1. Download Qt Creator for Linux:
sudo apt-get install qtcreator

If you installed Qt Creator in the default path, you can run it by calling:

qtcreator


Caution: if you do not run Qt Creator from the terminal, the path will not be set correctly and the building of your project will fail!

Use Qt Creator for your project

Now you have installed a proper IDE, you can start to do some real programming! In previous tutorials, we created a C++ project called my_project and went through a little bit of work to get it to build using CMake. Now, that work will pay off: Qt Creator 'understands' CMake, so we can directly load the project. Simply start Qt Creator and:


  1. From the home screen (Welcome tab on the left) select Open Project.
  2. Navigate to the CMakeLists.txt in your project and open it.
  3. Cick on "Details" for the Desktop setting and leave only the box "Default" checked. Click "Browse..." and select the build folder that you want the cmake output to be built in (e.g. myproject/build as in the previous tutorial)
  4. Hit the "Configure Project" button, you should now be able to compile and run using the green "Play" button in the left bottom.
  5. If QtCreator complains about a missing executable file, closing it and reopening from the terminal will likely fix this (it is a known isssue).


Qt Creator may seem daunting at first: there is a lot that you can do with it. However, we will just use some basic things, so don't worry. On the left, you see a menu with Welcome, Edit, Design, etc. You will mostly be using Edit. To the right of that, there is a window showing your project layout. You can see the name of your project, as specified in the CMakeLists.txt, you can see the CMakeLists.txt itself, and the src directory with example.cpp inside. From this point on you don't really have to leave Qt Creator any more. You can edit the source file in Qt Creator, edit the CMakeLists.txt, run CMake by right clicking the project name and selecting Run CMake, compile the project using ctrl-B and even run your program using the green 'play' button in the lower left. That's pretty awesome!

But you ain't seen nothing yet. Double click on example.cpp to edit it. Remove the #include statement at the top, and type it again, but slowly. You will see that as soon as you type the #, Qt Creator pops up a window with some suggestions. The more you type, the more specific it becomes. You can select an option using the arrow keys, and press enter or tab to confirm. This is called auto-completion and will save you a lot of typing. Continue typing. When you get to <em, you will see that Qt Creator even understands the location of header files, and auto-completes them for you.

Now lets make a mistake on purpose: misspell return and type ctrl-B to compile the project. You will see a list of issues pop up on the bottom, and a red dot besides the line that contains the error. You can double click on issues to directly jump to the error, which is especially useful if your project gets bigger.