Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add pre processing defs (macros) to qt creator?

Tags:

qt

qt-creator

In Eclipse there is an option to specify pre processing defines (#ifdef macros) to a project by using the Symbols option in Paths and Symbols. This helps in effective indexing of code which is cross platform. Is there any option to provide these in Qt creator?

like image 957
Bharathwaaj Avatar asked Sep 06 '10 09:09

Bharathwaaj


5 Answers

It depends:-)

The following is assuming you are using qmake based projects:

First you can add DEFINES += SOME_DEFINE=value into your .pro file. That is picked up inside creator and when building on the command line and should also show up when creating a MSVC or XCode project from the .pro file.

Then you can add DEFINES += SOME_DEFINE=value to the qmake call that Qt Creator will issue when configuring the project. That happens in the Project Mode, Build Settings, QMake Steps.

Finally, you can put #define SOME_DEFINE value liens into a header file and include that. That works for all kinds of projects:-)

like image 129
Tobias Hunger Avatar answered Nov 16 '22 13:11

Tobias Hunger


From the QT Documentation:

The defines are specified in the .config file. The .config file is a regular C++ file, prepended to all your source files when they are parsed. Only use the .config file to add lines as in the example below:

#define NAME value

That is, if you import a project named MyProject, then the pre-processor definitions should be specified in MyProject.config

For my projects it causes the indexer to recognize this define project wide and changed the auto-complete to reflect this.

like image 30
Gus E Avatar answered Nov 16 '22 12:11

Gus E


I think the initial answers are good, but they require that you manage your configuration manually whereas there're ways to let the IDE manage this for you automatically based on whether you have a release or debug configuration selected.

This bit may be redundant, but please note that this will work for you only if you are using the IDE for building. Obviously, if this is not the case, you will need a different solution.

Steps

Since pictures are worth a thousand words, here's an example of how you define a debug macro for your debug build using Qt Creator 4.3.1:

  1. Make sure you have your Debug configuration selected;

Run Configurations

  1. Go to the Projects section on the left menu;
  2. Go to the Build section

Projects >> Build

  1. Under Build Steps, look for the Additional arguments input box;
  2. Enter your macro definitions (e.g. DEBUG for your #ifdef DEBUGs in the code; in my case it's __CTS_DEBUG__)

Additional Arguments

The macro will now only be defined when you're using your debug config; when you choose your Release config (see step 1), it will become undefined automatically and your conditionally-compiled debug code will be removed, as shown in the pictures below, which is just what you always wanted.

Results

With the debug config selected

Debug Config

With the release config selected:

Release Config

like image 9
code_dredd Avatar answered Nov 16 '22 13:11

code_dredd


I wanted to specify a #define string in the .pro file, but my code ended up with the contents of the string without the quotes. I had to escape the quotes, and then escape the escapes to get one pair of quotes to last all the way to my code. This is because qmake strips off one set of escapes and some quotes, then the command line strips off the rest of them. This worked for me:

DEFINES += "VERSION=\"\\\"0.1.0\\\"\""

On Windows, this VERSION string can then be used in the .rc file to create the version stuff where Windows wants it and as well as in code for an "About" menu.

like image 7
Brandon Avatar answered Nov 16 '22 12:11

Brandon


You can define some PREPROCESSOR in the Project settings in QtCreator. I do not have QtCreator here but i remember there is a tab for project configuration.

like image 1
Patrice Bernassola Avatar answered Nov 16 '22 12:11

Patrice Bernassola