39

Where in Qt Creator do I pass arguments to a compiler?
It isn't really that obvious.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
smallB
  • 16,662
  • 33
  • 107
  • 151

6 Answers6

47

Depending on your build system it's either in your qmake project file(.pro, standard for new projects) or in one of the CMake files (CMakeLists.txt, used by KDE and several other projects).

Using .pro:

QMAKE_CXXFLAGS += -O2

Using CMake:

set( CMAKE_CXX_FLAGS "-g -Wall")
feedc0de
  • 3,646
  • 8
  • 30
  • 55
mbx
  • 6,292
  • 6
  • 58
  • 91
  • would you know, how to pass in .pro multiple args to a compiler? – smallB Nov 02 '11 at 15:43
  • 2
    You can just separate them by space `QMAKE_CXXFLAGS += arg1 arg2`. It is also possible to use multiple `QMAKE_CXXFLAGS += arg` lines, which is the usual way if you want e.g. platform specific build behavior. – mbx Nov 02 '11 at 19:17
  • 1
    Thanks for the cmake reference. – steventaitinger Sep 18 '15 at 18:26
  • Regarding CMake projects: While I agree that this is an answer to the original post, I miss the option to pass flags to CMake *without having to edit a project file*. When running `cmake` outside of Qt, one is able to pass flags like `-DCMAKE_CXX_FLAGS "-g -Wall"`. How can this be achieved in (newer versions of) Qt Creator?? I remember that this used to work in older versions of Qt Creator, (before 2012 or so), where one was able to pass such flags in a command prompt that showed up when opening a CMake project in Qt. – normanius Dec 20 '17 at 10:24
  • 1
    Just found [this documentation](http://preshing.com/20170511/how-to-build-a-cmake-based-project/#building-with-qt-creator) that shows how it used to work in Qt Creator v3.x. I'm so terribly missing this feature in Qt Creator v4.x :( – normanius Dec 20 '17 at 10:33
8

To add compiler flags, open your .pro file and add a line like this:

QMAKE_CXXFLAGS += -std=c++0x

For standard flags like debug vs. release etc. you should try to use the predefined qmake options (see QMake documentation) for the sake of platform and compiler-independency, as QMake will map them to the compiler-specific flags.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • thanks, great pity you cannot pass them via some dialog. thanks anyway. – smallB Nov 02 '11 at 13:54
  • 1
    That dialog would probably more complicated to use than just opening the text file and adding the line. And wouldn't work with any advanced logic. Maybe it's because I'm from the Unix world, but I've never seen a UI that could handle the build system complexity of a real-world project, in a convenient way. Also you lose the main feature of any good build system: The ability to mindlessly copy&paste ;) – Frank Osterfeld Nov 02 '11 at 16:47
4

If your intention is to precompile some source code you can do like this:

/A/ In your .pro file you can add a line like this:

DEFINES += HOPLA

/B/ In you .cpp or .h file you can use it like this

#ifdef HOPLA
// Do something
#else
// Do something different
#endif
lolo67
  • 184
  • 1
  • 1
  • 7
3

for C projects, add the following line in .pro file

QMAKE_CFLAGS += -std=c99
lalitm
  • 626
  • 9
  • 11
2

As the elected answer points, for CMake based projects, you can edit the CMakeLists.txt and set the flags for the compiler, and for this case, I have a pictorial demonstration on how to add flags on QtCreator/CMake.

I wanted to add the '-pedantic' flag, which warns about extensions, without throwing errors while executing the program, and here's pictorial example of how to enable compiler flags on CMake while using QtCreator:

enter image description here

For more context:

On the example below, I'm setting the size of a Static Array at Runtime, something that is only possible with Variable-length Array feature, which is available at C99, but defined as optional feature starting from C11. Without -pedantic flag being available for the compiler, the warning would't be displayed after compiling the code.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
1

in the .pro file you can add variables which modify the make behavior for example, if you try to execute the following command:

g++ -Wall -I/usr/include/cppconn -o exe main.cpp -L/usr/lib -lmysqlcppconn

you must add the following lines in the .pro file

INCLUDEPATH += /usr/include/cppconn
LIBS += -L/usr/lib -lmysqlcppconn

Check the image below. enter image description here

For more information on the available variables that QT IDE uses, you can visit the following link where they explain in more detail each one. Qt Documentation: Variables

Mike
  • 706
  • 7
  • 16
Bayron Vazquez
  • 321
  • 3
  • 9