Where in Qt Creator do I pass arguments to a compiler? - c++

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

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

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.

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

for C projects, add the following line in .pro file
QMAKE_CFLAGS += -std=c99

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.
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

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

Related

Qt debugging in release mode - all methods not working

I am trying to build my Qt app with MinGW in QtCreator in order to try and debug a release-only crash.
Here is what I put in my .pro file (also tried putting it directly into qmake.conf):
QMAKE_CFLAGS_RELEASE += -g
QMAKE_CXXFLAGS_RELEASE += -g
QMAKE_CXXFLAGS += -g
as well as tried with
QMAKE_CFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS += -ggdb
I tried them separately or together like this:
QMAKE_CFLAGS_RELEASE += -g
QMAKE_CXXFLAGS_RELEASE += -g
QMAKE_CXXFLAGS += -g
QMAKE_CFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS += -ggdb
I have also manually edited the generated Makefile(Makefile.Release to be exact and checked Makefile.Debug just in case) to remove the NO_DEBUG flag (I don't know why it was being put there) and to make sure the -g and -ggdb flags are there.
The result of all of this is that the app took almost twice as long to build, however it was exactly the same size as before and didn't have any debugging symbols and trying to debug it didn't succeed.
What else can I do? I know it's possible, but looking for other answers only directed me to using those flags and that didn't help at all(only made build slower)
In qmake.conf (can't recall where this is now) the default behaviour is set as:
QMAKE_LFLAGS_RELEASE = -Wl,-s
Where the -s means the linker does away with the symbolic info. So in addition to the flags you have set you need to clear the flags (or overwrite) in QMAKE_LFLAGS_RELEASE. In your case just setting it to empty should do it, so in your .pro file add:
QMAKE_LFLAGS_RELEASE =
FURTHER NOTES
If you add the line message(QMAKESPEC: $$QMAKESPEC) to your .pro file, when you run qmake you will see the path for your make spec. Look in the qmake.conf file (it will have includes) and work your way through them.
Assuming you are using windows (because I did not see the -Wl,-s option set in the linux make specs for Qt 5.5.1) then the file in question is:
<path-to-qt>/5.5/gcc_64/mkspecs/win32-g++/qmake.conf
Anyway, I did not see any other useful flags set (I am using linux so this was only checked by eye. However if you are worried, then add the following to your .pro file:
message(QMAKE_LFLAGS_RELEASE: $$QMAKE_LFLAGS_RELEASE)
To check it before you make changes and then you will have the complete picture of what is going on :)
I've just figured it out, and only saw #code_fodder's answer after it. It looks like #code_fodder is essentially right, but I'm adding more details to reflect the precise steps that helped me.
I've specified qmake arguments in the Qt Creator project settings like this:
QMAKE_CXXFLAGS+=-g QMAKE_LFLAGS_RELEASE-=-Wl,-s
This essentially adds -g to the compiler flags and removes -Wl,-s from the linker flags (which strips all symbols). Then I ran qmake and did full rebuild. After that, debugging sort of worked—as you'd expect for a release build: jumping from line to line in a somewhat random fashion because of the optimizations, but at least it worked.
I'm using 32-bit Qt 5.5.1 with bundled MinGW on a 64-bit Windows 7 system, if that is of any help.

Adding a library to a makefile

I just installed RtMidi for a project and compiled it. The examples in the tests folder work and so does my code if I put it in the folder and include it in the Makefile that compiles all the examples. How can I use RtMidi in a project with #include <RtMidi.h> instead of having my code in the tests folder? More specifically, what should I put in my Makefile? I've read a bit about dynamic and static libraries but I have no idea what I should be looking for. I've tried adding -llibrtmidi and /usr/local/lib/librtmidi.a without success.
In a standard Makefile, the CXXFLAGS macro defines flags for the C++ compiler. You will need to add -I<path to header directory> to this macro for the compiler to find the RtMidi header files.
Then you will need to add -L<path to lib directory> to the link step of the Makefile so that -lrtmidi will find the library file. (Note that you omit the lib prefix for the -l command)
Based on your description of your environment, you may require something like
CPPFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib
LDLIBS += -lrtmidi
in your Makefile. make uses a lot of these implicit variables.

qmake: compile single cpp file into an executable

I got a project in QT and I'd like to use qmake for compiling one additional cpp file that (into standalone executable) is not connected in any way to my application or even QT (it's very simple plain C++ program). Is there any way to do this without rebulding whole project structure? Do I need separate .pro file for every executable or is there any other way for simple compiling just one, plain C++ file?
As you may know qmake -project will make one .pro file with the name of the folder containing your whole source and header files, if you qmake this pro file then make your project you will get compiled .o file from your new cpp file even if it's not connect to your Qt project directly.
but if this file got main() function of course you will have multiple main() definitions error by compiler.
you will need to rebuild that file of course
as you know for simple compiling of only one standard plain c++ file you just
g++ source.cpp -o excutable_name.exe
for more strict compiling with two steps:
g++ -Wall -pedantic -ansi source.cpp -c compiled_file_name.o
g++ compiled_file_name -o excutable_name.exe
but if you are going to use for example a code related to Qt, you have to include Qt headers and link necessary libraries :
g++ -Wall source.cpp -c compiled_file_name.o -L qt/library/path -lQtGui -lQtCore -lQtother_necessary_libraries -I Qt/include/path -I Qtother_necessary_include_paths
To make an additional executable you can use use system() in .pro like so:
system(g++ otherapp.cpp)
Which will be built every time you call qmake. However if you want to build the additional app automatically when its source is changed, use QMAKE_EXTRA_TARGETS instead.

how to makefile with iostream, string and ifstream library

I'm trying to build a makefile for an application that use the lib string iostream and fstream.that's what I did up to now
CPP = gcc
LIB_DIR = ./incl
PROGRAMS = test
PROGS_O = action_rec.o
CPPFLAGS = -I$(LIB_DIR) -pg -g
VPATH = ./src/
OBJFILES = $(VPATH)$(patsubst %.cpp,%.o,$(wildcard *.cpp))
LIBS = -02 -liostream -lfstream -lstdlib -lstring
when I try to use my makefile I get as result that everything that need the lib string fstream and iostream was not declared, while everything that need the lib stdlib works properly.
can someone tell me why?
thank you
Try taking all the -lstring -stdlib -liostream -lfstream out and see if it fixes it.
And compile with g++ so it finds other things it needs to link C++ code.
There are two separate issues here.
The C++ side:
Don't use the gcc binary for compiling C++ code - use g++ instead. This will pull in all the required standard include paths and libraries automagically.
The Makefile side:
Your Makefile has a number of issues:
Don't set CPP to a compiler; this variable defines the preprocessor to use. Use CXX to define the compiler - it most likely happens to be defined to g++ anyway.
Don't create programs with the name test. This happens to clash with a standard Unix program (/usr/bin/test) and if you don't pay attention you might end up calling the wrong program.
Don't pass -02 as the linker flags via LIBS. This switch is a) wrongly worded (it should be -O2, the letter O not a zero) and b) it's a compiler flag, so it should be part of CXXFLAGS.
Chances are that you don't need a Makefile at all. The make program comes with default Makefiles so often you can just create a main.cpp file and then run
make main
This will automatically compile and link main.cpp into a main executable. In your case, something like
CXXFLAGS="-I./incl -pg -g -O2" make main
Might be sufficient.
The obvious problem is that you're linking with gcc rather than g++.
gcc doesn't link against the C++ standard library. You can add it
manually, but the simplest is simply to use g++ for everything (unless
you have actual C code; g++ will compiler files ending in .c as
C++).
LIBS = -02 -liostream -lfstream -lstdlib -lstring can be simplified a lot. Just use LIBS = -O2 (I think you meant -O2 rather than -02.) In fact, you really shouldn't be passing a -O flag to the link stage at all. Optimization is a compile-time option, not a link-time option.

Qt and gcov, coverage files are not generated

I am trying to obtain code coverage for a component I am writing for the Arora browser, that is written using C++ and Qt framework.
I am not able to use the gcov program, neither under Gnu/Linux nor Mac Os X. I tried everything I was able to find on the Internet, also by forcing things automatically editing the Makefile generated by the .pro file.
Can somebody help me please? This is my very simple pro file:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
LIBS += -lgcov
QMAKE_CXXFLAGS += -g -fprofile-arcs -ftest-coverage -O0
QMAKE_LDFLAGS += -g -fprofile-arcs -ftest-coverage -O0
include(../autotests.pri)
# Input
SOURCES += tst_quickview.cpp
HEADERS +=
The Makefile does correctly contain the flags. I also tried the --coverage option. But nothing happens. When I run the executable, no gcov files are generated. There are no errors and no warnings. I am using the QTestLib framework.
Thank you
Atleast in cases where i've had similar issues, the problem has been in directory structures.
First issue was/is that in order to generate proper .gc* files during execution, compiler cache needs to be disabled. I never really debugged the issue but now, i'd assume that the gconv files where infact placed on the compiler cache folder and during the execution of the coverage instrumented binaries, binary was not able to idenfify where new datafiles should be generated.
Second issue is/was that i had to run the tests in the same directory structure as they where compiled in. For example, if i had compiled the application in "/home/foo/src/myproject/" and all its subdirectories .. The execution has to happen in that same directory structure or the datafiles won't get generated..
At least you should use
QMAKE_LFLAGS += -g -fprofile-arcs -ftest-coverage -O0
instead of
QMAKE_LDFLAGS += -g -fprofile-arcs -ftest-coverage -O0
I'm not sure if that will fix your problem but QMAKE_LDFLAGS is not going to do anything useful.