Qt and gcov, coverage files are not generated - unit-testing

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.

Related

Create Makefile to be used in different environments for C++

I would like to compile and run my program in two different environments. The libraries in both environments are installed on slightly different places, resulting in different makefile-lines:
In makefile A:
CXXFLAGS=-I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I/opt/interp2d/include -std=c++17 -I/opt/splinter/include -I/usr/include/eigen3
In makefile B:
CXXFLAGS=-I$(DIR) -nostindc++ -I~/local_opt/eigen/include/eigen3/ -I~/local_opt/boost/include -I~/local_opt/armadillo/include -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I~/local_opt/interp2d/include -std=c++17 -I~/local_opt/splinterp/include -I/usr/include/eigen3
My problem now is that I am developing the program on the first machine, using makefile A, but also deploying it on the second machine. The deployment is done using git.
Every time I do a git pull on the second machine, I have to fix all the paths in the makefile in order to compile the program properly. Nevertheless I still would like to include the makefile in the git repository in order to keep both makefiles at the same level regarding compiling flags and linked libraries.
Thus, is there an easier way to still sync the makefile via git, while using different paths for the libraries and includes?
I think you could solve your problem by conditionally setting the variable CXXFLAGS in a common file (e.g.: config.mk) and by including that file in your makefiles.
The value used for setting the CXXFLAGS variable could, for example, depend on the value of the environment variable HOST:
ifeq ($(HOST),A)
CXXFLAGS = ... # for machine A
else # B
CXXFLAGS = ... # for machine B
endif
Then, include this config.mk makefile in both makefileA and makefileB:
include config.mk
I like this answer, however, I thought I'd mention this for completeness: If you have a lot of different hosts you can do something to the effect of:
include HostConfig_$(HOST).mk
And then create HostConfig_A.mk and HostConfig_B.mk which set host specific flags (Be it directories, etc). This is useful if you are managing a large project with lots of different host-specific variables.
As well, (for smaller projects), you could do something to the effect of:
CXX_INCLUDES_A = ...
CXX_INCLUDES_B = ...
CXX_FLAGS := -I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17
CXX_FLAGS += $(CXX_INCLUDES_$(HOST))
The traditional answer to this problem is a configure script (see automake, autoconf for widely used framework). After checking out the source you run ./configure --with-eigen=~/local_opt/eigen/include/eigen3/ and it will adjust your Makefiles accordingly (usually generates Makefile from Makefile.in and only Makefile.in is in git).
Note: Properly done you only need to run configure on the first checkout, not on updates. make can generate Makefile again automatially as needed.

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.

Compiling standalone Qt application using MinGW

I am trying to build a standalone Qt app without any DLLs needed. I recompiled Qt 5.4.1 statically. When I compile and run an application, it doesn't require any Qt DLLs, but it requires libgcc_s_dw2-1.dll instead. I have also edited my mkspecs before configuring and building Qt, I edited these values:
QMAKE_CFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc
QMAKE_CXXFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc -static-libstdc++
(added -static -static-libstdc++ and -static-libgcc)
I also added a QMAKESPECS environment variable.
When I build something using Qt, I can always see this options in the output, so I am sure that the mkspecs are applying.
When I build a non-Qt program with these options (-static -static-libgcc -static-libstdc++), it doesn't need any DLLs when I run it.
Can somebody help me?
I use Qt 5.4.1 and MinGW-w64 4.9.2
I solved my problem now. The problem was that although I edited the variable QMAKE_CXXFLAGS, it was still linking the standard libraries dynamically when linking the application itself, because it doesn't use this variable in the final step of the compilation. I only edited the mkspecs again and added the -static option to the variable QMAKE_LIBS and it works now, I have a standalone Qt application.

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

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.

CXXFLAGS modification of Qt pro file? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Configuring the GCC compiler switches in Qt, QtCreator, and QMake
I would like to use -O1 instead of -O2 in my makefile (CFLAGS and CXXFLAGS) for my Linux build. My understanding of how these makefiles are generated based on the .pro file is somewhat lacking. This is because the version of Qt combined with the version of G++ I am using has instabilities when -O2 is present.
Presently, I am running a replacement script, after I run qmake, which does this:
sed -i 's/\-O2/\-O1/g' AllProjects/Makefile.Release
This is a ghetto solution. A much better solution would be to modify the .pro file somehow to pass along these directives. I am not sure how CFLAGS and CXXFLAGS are being generated though.
I have tried passing a
linux-g++-{
CFLAGS += -O1
CXXFLAGS += -O1
CONFIG += -O1
}
which did not work.
You were very close. What you want is:
QMAKE_CXXFLAGS += -O1
If you would like to apply flags to just the release build, then you can use this:
QMAKE_CXXFLAGS_RELEASE += -O1
You also probably want to change your condition to be a little more flexible. In summary, something like this:
*-g++* {
QMAKE_CXXFLAGS += -O1
}
More in the documentation here: http://qt-project.org/doc/qt-5.0/qtdoc/qmake-variable-reference.html#qmake-cxxflags