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

6

39

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?

Fawcette answered 6/9, 2010 at 9:42 Comment(0)
U
17

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.

Unleavened answered 28/11, 2011 at 16:15 Comment(3)
I've not tried this. I no more work in Qt. But I think from your answer this might be right. So accepting it as of now for others use.Fawcette
This works when I do as the docs say and "Only use the .config file to add lines as in the example below". I was hoping that "The .config file is a regular C++ file" meant that I could also use #ifdef/#else/#endif in there, but gives odd results. Is that supposed to work?Pictogram
I am unable to locate the .config file with the latest Qt version. How to locate that? The documentation link seems to not much of use.Claud
R
42

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:-)

Remora answered 8/9, 2010 at 14:22 Comment(6)
No I don't use qtcreator for building. I wan't it only for its powerful indexing.Fawcette
And my project is not qmake based.Fawcette
+1 This may not help the original poster, but this is the top hit for "qtcreator defines" on google and many of us are using qmake and building in qtcreator. Thanks!Wiley
If you are using a generic project: You can add defines to PROJECTNAME.defines, if you are using cmake: Add defines to your CMakeLists.txt, if you are using autotools: You are out of luck IIRC, Creator can not get the defines from configure.ac:-) But then those tend to end up in some header file anyway which is included. That will work great, too.Remora
When adding the define to the .pro file, you will have to do a clean and rebuild in qtcreator. Hopefully this tip might save someone 10 minutes one day.Micropyle
@BrandonYates I don't think there's a way to add global define without recompiling the whole project.Medeiros
U
17

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.

Unleavened answered 28/11, 2011 at 16:15 Comment(3)
I've not tried this. I no more work in Qt. But I think from your answer this might be right. So accepting it as of now for others use.Fawcette
This works when I do as the docs say and "Only use the .config file to add lines as in the example below". I was hoping that "The .config file is a regular C++ file" meant that I could also use #ifdef/#else/#endif in there, but gives odd results. Is that supposed to work?Pictogram
I am unable to locate the .config file with the latest Qt version. How to locate that? The documentation link seems to not much of use.Claud
P
12

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

Pinniped answered 22/12, 2017 at 5:0 Comment(3)
This solution is not optimal because these are local settings for QtCreator. Each programmer has to replcate this work in order to compile.Atombomb
@Atombomb The question is very explicit about it being specific to QtCreator. My answer deals with that directly and shows how to do this in QtCreator.Pinniped
@Pinniped actually you're right. I take that back.Atombomb
G
7

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.

Granada answered 29/4, 2016 at 22:6 Comment(1)
I had a similar problem. In bash on the command line, this worked for me: qmake DEFINES+="BUILD_DATE='\'\"August 17, 2016\"\''"Vinia
C
1

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.

Caseate answered 6/9, 2010 at 10:58 Comment(6)
I couldn't find such setting in the Project Configuration.Fawcette
The Tab name is "Projects", You can custom compilation here by adding your preprocessor defineCaseate
I don't use qt creator for compilation. I use it for indexing.Fawcette
You can add preprocessor in compiler parameters using the \d option. Example: g++ -D NAME[=Value]Caseate
This won't work. I don't use qt creator for compilation. I use it for indexing. Looks like qt creator doesn't have that functionality to statically interpret code.Fawcette
Compiler option are not a feature of QtCreator but a compiler one. Even if you are not using QtCreator for compiling your code, you can define PREPROCESSOR with the -D option and use them in your code in QtCreator.Caseate
B
1

First suggestion from @Tobias Hunger's answer worked for me. I was doing this in my .pro file:

DEFINE += KEY=value

which did not work until I changed it to:

DEFINES += KEY=value

That said, if you want to do everything from the command line, an almost hybrid solution that does not use Qt Creator GUI, but does use .pro files, is described here:

https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/

Using the method described in the above link, you can use qtcreator -project to produce a .pro file, then use notepad or vim or some other text editor to add the DEFINES += KEY=value line to the .pro file you just created. Then use qmake thusly: qmake <your_project>.pro, to do the Qt pre-processing, and finally just make to build an executable.

I've also heard lore of a qtcreator -D option to add preprocessor defines from the command line as described here: https://doc.qt.io/archives/qt-4.8/qmake-variable-reference.html#defines but I've never tried it.

Hope something works for you! Your dedication to the CLI is admirable.

Baudekin answered 25/2, 2020 at 1:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.