Difference between revisions of "HowTo Use OpenFOAM with QtCreator"

From OpenFOAMWiki
(Autocomplete)
(Autocomplete)
Line 86: Line 86:
 
[[File:Qt-IncludePrototypesDirectory.png|center|600px|Include directories with header files (.H)]]
 
[[File:Qt-IncludePrototypesDirectory.png|center|600px|Include directories with header files (.H)]]
  
Notice that Qt Creator will only be able to autocomplete in files which call the respective definition. OpenFOAM coding style often include standard routines inside the algorithm by ''#include'' calls. The compiler accepts this, but it is not a good programming practice. In this case, if you try to edit the routine file ''createFields.H'', you will not have autocompletation of things defined in ''fvCFD.H''. Autocompletation will work only in the file where ''#include "fvCFD.H"'' written.
+
Notice that Qt Creator will only be able to autocomplete in files which call the respective definition. OpenFOAM coding style often include standard routines inside the algorithm by ''#include'' calls. The compiler accepts this, but it is not a good programming practice. In this case, if you try to edit the routine file ''createFields.H'', you will not have autocompletation of things defined in ''fvCFD.H''. Autocompletation will work only in the file where ''#include "fvCFD.H"'' is written.
  
 
Auto-complete can either be set automatic or by pressing
 
Auto-complete can either be set automatic or by pressing

Revision as of 14:34, 1 September 2012

Qt Creator is a powerful and flexible cross-platform integrated development environment (IDE) with strong orientation and support for C/C++ native development.

1 Why to use Qt Creator?

  • Open Source
  • Cross Platform – Works perfectly on Linux, Windows and Mac OS X
  • Direct compatibility with other applications of Qt SDK (great for building GUI-applications!)
  • Actively developed and improved
  • Excellent syntax highlighting
  • Magnificent code browsing and navigation in combination with useful and customizable keyboard shortcuts
  • Great and generic (works with any file you include, it doesn’t have to know anything special about it)
  • Code completion support
  • Easily customizable and Extensible through plugins
  • Support for integration with various Source Control Management tools like Git, Subversion, Bazaar, Mercurial, CVS and more.
  • Debugger integration with both GDB and Microsoft’s Debugging Tools for Windows through CDB
  • Custom configure, build, clean and deployment steps
  • Integration with tools like Valgrind
  • Per project settings
  • Vim editing mode
  • Code snippets
  • A pretty fast IDE, even when indexing hundreds of files with hundreds of thousands lines of code for the first time
  • Linux Man Pages context help integration - thanks to the flexible Qt Help System, this can be extended to almost any API

2 Download and installation

Most of the main Linux distributions have binaries in their repositories. Otherwise, download and install Qt Creator from http://qt.nokia.com/downloads.

Installation example in Debian/Ubuntu:

# apt-get install qtcreator

3 OpenFOAM project in Qt Creator

Obs.: Using Qt Creator version 2.5.0

3.1 Starting a project

  1. Open Qt Creator.
  2. Go to File \to New File or Project \to Other Project.
  3. Click on Import Existing Project.
    Import existing project
  4. Give your project a name and select the location to your project source tree. Select the directory where the source-files (*.C, *.H) of your OpenFOAM application are. A project-file <project name>.creator will be created inside the selected directory.
    Give a name and directory location
  5. A screen will pop up asking you to select the files to be added to the project.
    Selection of files for the project
  6. The next screen, Project Management, is going to ask you if you want to use source control management (git, svn) for your project and if yes,then the project files will be the first to be added to the project. You should add these files to the scm repository only if you know that they would be usefull for others, otherwise you should keep them to your self, by using exclude directives like .gitignore files in git or svn:ignoreproperty in svn.
    Selection of control management application

If everything went well, your project is ready.

3.2 Configuring for wmake

You should now have your project open in Qt Creator and ready to proceed to project settings. On the left side of Qt Creator click on Projects. Right now you’re looking at the Build Settings screen. This is where you set your project building and cleaning steps.

Configure build settings
  • Build directory is your reference directory stored in %{buildDir} variable.

You have probably configured your OpenFOAM to work in the shell terminal by adding the command

source /usr/local/OpenFOAM/OpenFOAM-2.1.0/etc/bashrc

to your ~/.bashrc script-file. This exports environment variables pointing to libraries and executables directories of a selected OpenFOAM version. However, ~/.bashrc-script will not be able run if you launch Qt Creator from the window manager and you will not be able to simply call wmake to compile your application. Yet, there are several ways to use wmake.

3.2.1 Option 1: using foamExec

The cleanest in my opinion. Run the command foamExec with command argument wmake or wclean, as shown in the Figure above. This foamExec-script will load any OpenFOAM application of its same version (in this case 2.1.0).

3.2.2 Option 2: write your own script

Other less advisable option is to write your own shell script like the makeScript also shown in the same Figure. Notice that this option was disabled there, so only the first is run by the Build Project command.

#!/bin/bash
#Adding OpenFoam to shell environment
source /usr/local/OpenFOAM/OpenFOAM-2.1.0/etc/bashrc 
if [ "$1" = clean ]; then 
   echo wmake\: cleaning...
   wclean
   exit
else
   if [ "$1" = build ]; then 
       echo wmake\: building...
       wmake
       exit
   else
       echo $0\: \*\*\* No rule to $0 target \'$1\'.  Stop.
       exit
   fi
fi

3.2.3 Option 3: Add environment variables manually

well... more work... You can call wmake and wclean from OpenFOAM's bin directory.

You may now be able to build (ctrl+b) and clean the project.

3.3 Autocomplete

Autocomplete will only work if Qt Creator sees the header files (.H) with the prototypes of the classes, functions, typedef and namespaces you are using. Add the directories where these header files are to your project in the file <project name>.includes. Each line corresponds to one directory.

Include directories with header files (.H)

Notice that Qt Creator will only be able to autocomplete in files which call the respective definition. OpenFOAM coding style often include standard routines inside the algorithm by #include calls. The compiler accepts this, but it is not a good programming practice. In this case, if you try to edit the routine file createFields.H, you will not have autocompletation of things defined in fvCFD.H. Autocompletation will work only in the file where #include "fvCFD.H" is written.

Auto-complete can either be set automatic or by pressing

ctrl+space

3.4 Running application

Here we have the same problem of not having the OpenFOAM environment variables loaded to Qt Creator. The same solutions apply (foamExec, makeScript, etc.). Set a test case directory in the terminal and do not forget to prepare it by generating the mesh (blockMesh...) and configuring the dictionaries.

Note: there was problem when I tried to Run in terminal. There was a bug preventing my Qt Creator to connect with gnome-terminal [1]. My version of Qt Creator runs only with xterm -e, which is not the nicest terminal interface. But works well. You can change the terminal application in: Tools \to Options... \to Environment \to General. In System:Terminal write:

xterm -e

3.5 Debugging

4 References

This tutorial is based on:

  1. [https://bugreports.qt-project.org/browse/QTCREATORBUG-1633 Bug report: Running in terminal works only with XTerm