Gtest with lcov in c++ - c++

I have successfully generated the .gcda and .gcno files and they are in the same folder.
The flags in the make file looks as below :
CPPFLAGS += -std=c++11 --coverage -isystem $(GTEST_DIR)/include
CXXFLAGS += -std=c++11 --coverage -g -Wall -Wextra -pthread -I$(PULSE_DIR)/inc
CFLAGS += -std=c++11 --coverage -g -Wall -Wextra -pthread -I$(PULSE_DIR)/inc
I am using the googleTest (Gtest) to write the unitTests. The executable files generated for the unitTests are working fine. I just want to generate the code coverage.
I am successfully able to generate .gcno and .gcda files and both of them are in the same obj folder.
Then, I used the following lcov command from obj directory :
lcov -t "result" -o testcoverage.info -c -d .
This was supposed to generate testcoverag.info file but it gives me error as follows :
Capturing coverage data from .
Found gcov version: 5.4.0
Scanning . for .gcda files ...
Found 4 data files in .
Processing GeneratorObj_unittests.gcda
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-internal.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest-printers.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest.h
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-port.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest-message.h
Processing GeneratorObj.gcda
Processing gtest-all.gcda
Cannot open source file deps/googletest/googletest/src/gtest-typed-test.cc
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-port.h
Cannot open source file deps/googletest/googletest/src/gtest.cc
Cannot open source file deps/googletest/googletest/src/gtest-death-test.cc
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-internal.h
Cannot open source file deps/googletest/googletest/src/gtest-internal-inl.h
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-death-test-internal.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest-printers.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest-message.h
Cannot open source file deps/googletest/googletest/include/gtest/gtest-test-part.h
Cannot open source file deps/googletest/googletest/src/gtest-printers.cc
Cannot open source file deps/googletest/googletest/src/gtest-port.cc
Cannot open source file deps/googletest/googletest/src/gtest-test-part.cc
Cannot open source file deps/googletest/googletest/src/gtest-filepath.cc
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-filepath.h
Cannot open source file deps/googletest/googletest/include/gtest/internal/gtest-param-util.h
Processing gtest_main.gcda
Cannot open source file deps/googletest/googletest/src/gtest_main.cc
Cannot open source file deps/googletest/googletest/include/gtest/gtest.h
Finished .info-file creation
GeneratorObj_unittests is the unittest file that I have written. It some how is not able to open all the files associated with gtest folder.
If I could only generate testcoverage.info file then I would be able to generate the html coverage file using the genhtml.
I tried checking the permission of the gtest files and they are all read and write. I dont know what is going on wrong here.

Related

CMake Gcov c++ creating wrong .gcno files

I have a CMakeLists.txt file in which I added:
set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage -pthread -std=c++11 -O0 ${CMAKE_CXX_FLAGS}")
It is generating the report files in:
project_root/build/CMakeFiles/project.dir/
BUT the files it generates have extentions .cpp.gcno, .cpp.gcda and .cpp.o.
Also, they are not in the same folder as the src files, which are at:
project_root/src/
When I move the report files to the src/ folder and execute
$ gcov main.cpp
main.gcno:cannot open notes file
But I get that error message. So I change the .cpp.gcno, .cpp.cdna and cpp.o to .gcno, .gcda and .o and finally I get the following:
gcov main.cpp
Lines executed:86.67% of 15
Creating 'main.cpp.gcov'
I have over 50 files and can't do this manually for each one.
I need to be able to run gcov once for all files and generate report for all files. I don't care where the files are generated.
It is generating the report files in: project_root/build/CMakeFiles/project.dir/
This is directory where all additional files are built for 'project' executable.
BUT the files it generates have extentions '.cpp.gcno', '.cpp.gcda' and '.cpp.o'
This is because CMake creates .cpp.o object file from .cpp source (you may see that running make VERBOSE=1. In accordance to -fprofile-arcs option's description, data file has suffix .cpp.gcno.
Also, they are not in the same folder as the src files
Data files are created in the same directory with object file.
Actually, created files are still work, if you call
gcov main.cpp.gcno
from the directory with .gcno files.
Apparently the standard CMake behavior to add an extension to give .cpp.o can be changed to replace an extension to give .o by using:
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE ON)

C++ error on linux machine: ccpentium: No input files

I am writing a code for MRI scanner where all calculation is being done on a Windows machine, while waveforms are being played using linux.
Inside my code, when I declare
#include <vector>
and try to compile it on linux, I start getting errors like this:
ccpentium: FLASH.cpp: No such file or directory
ccpentium: No input files
Any help would be appreciated.
More detailed error:
export PATH=z:/n4_fsp/tornado/i86/host/x86-win32/bin:C:/WINDOWS/system32:C:/WINDOWS:C:/WINDOWS/system32/Wbem; z:/n4_fsp/tornado/i86/host/x86-win32/bin/ccpentium.exe -O1 -Wall -DTOOL_FAMILY=gnu -DTOOL=gnu -ansi -DVXWORKS -DUNICODE -D_UNICODE -mcpu=pentium -march=pentium -fvolatile -nostdlib -fno-builtin -fno-defer-pop -DCPU=PENTIUM -malign-double -D_CONSOLE -DCEST_OffsetSeries -DBUILD_SEQU -DBUILD_CEST_OffsetSeries -Iz:/n4/pkg -Iz:/n4/tool -Iz:/n4_prod/i86/prod/include -Iz:/n4_deli_vb15a/i86/del
ivery/include -Iz:/n4/x86/prod/include -Iz:/n4/x86/delivery/include -Iz:/n4/pkg/MrServers/MrMPCUSystem/Tornado_i86/target/h -Iz:/n4_extsw/x86/extsw/include -o FLASH.oi86 -c FLASH.cpp
In file included from z:/n4_fsp/tornado/i86/host/x86-win32/lib/gcc-lib/i586-wrs-vxworks/2.9-PentiumIII-010221/include/syslimits.h:7,
from z:/n4_fsp/tornado/i86/host/x86-win32/lib/gcc-lib/i586-wrs-vxworks/2.9-PentiumIII-010221/include/limits.h:11,
from z:/n4_fsp/tornado/i86/host/x86-win32/include/g++-3/stl_algobase.h:49,
from z:/n4_fsp/tornado/i86/host/x86-win32/include/g++-3/algobase.h:36,
from z:/n4_fsp/tornado/i86/host/x86-win32/include/g++-3/vector.h:30,
from FLASH.cpp:33:
z:/n4_fsp/tornado/i86/host/x86-win32/lib/gcc-lib/i586-wrs-vxworks/2.9-PentiumIII-010221/include/limits.h:117:60: limits.h: No such file or directory
ccpentium: FLASH.cpp: No such file or directory
ccpentium: No input files
ccpentium: FLASH.cpp: No such file or directory
ccpentium: No input files
ccpentium: FLASH.cpp: No such file or directory
ccpentium: No input files
idea_make: *** [FLASH.oi86] Error 1
20:03:23 ERROR sde::fDos: <idea_make.exe --unix -f CEST_OffsetSeries.mk i86Release> failed with status=2
20:03:23 ERROR sde idea_make.exe --unix -f CEST_OffsetSeries.mk i86Release failed
20:03:23 INFO sde Copying \n4\i86\prod\lib\CEST_OffsetSeries.i86 to c:\Temp\CEST_OffsetSeries.i86
1 Datei(en) kopiert.
The error is caused by the combination of
-nostdlib
flag which asks the compiler not to use its standard linking environment and missing files from your include paths.
From very recent experience, I was missing two files from the include:
-Iz:/n4/pkg/MrServers/MrMPCUSystem/Tornado_i86/target/h
Eventually, I manually copied them from an updated version, but I do not know whether this is a bug or a 'feature' of the older version, so it still remains to be tested whether the compiled code does not break the scanner.

how to successfully run a c++ file which uses ogdf libraries

I compiled the file (source.cpp) using the command
g++ -I/home/hrishikesh/Desktop/OGDF-snapshot/include -O2 source.cpp -o mytest -L/home/hrishikesh/Desktop/OGDF-snapshot/_release -lOGDF -lCOIN -pthread
and it got compiled successfully without giving any error message,resulting a file "mytest" in the same folder as the source.cpp in.
when I try to run the mytest file using command
./mytest
it shows this error message
./mytest: error while loading shared libraries: libOGDF.so: cannot open shared object file: No such file or directory
please help
You need to put libOGDF.so in the same folder than mytest
g++ -I /AbsolutePath/Desktop/OGDF/ main.cpp -L -l
/AbsolutePath/Desktop/OGDF/_release/libOGDF.a -lpthread
here main.cpp is the file.
copy and paste the line above in other text editors to get rid of the confusions between the usage of I and L and the spaces.
Be sure that you are writing the absolute path correct.
The a.out file will be generated in current directory. execute it by using:
./a.out

gcov on larger projects (static libraries, ...)

I'm working on larger project which has the following directory layout:
Source
MyA
aa.cpp
ab.cpp
ac.cpp
MyB
ba.cpp
bb.cpp
bc.cpp
MyTest
testaa.cpp
testab.cpp
testac.cpp
testba.cpp
testbb.cpp
testbc.cpp
main.cpp
Build
MyA
aa.o
ab.o
ac.o
libMyA.a (static library)
MyB
ba.o
bb.o
bc.o
libMyB.a (static library)
MyTest
testaa.o
testab.o
testac.o
testba.o
testbb.o
testbc.o
MyTest (executable)
After compiling with -fprofile-arcs -ftest-coverage I execute the MyTest application inside the Build/MyTest directory. As expected there are *.gcno and *.gcda files inside the Build directory. After running gcov inside the MyTest directory different *.gcov files are produced but unfortunately not for everything inside MyA and MyB, although every function is called inside this two libraries. Tried different options but somehow I'm unable to create useful (means correct) *.gcov files with this layout.
If I copy every cpp inside one directory and repeat the steps everything works as expected and the coverage analysis is perfect.
You must specify source files as absolute paths to g++/gcc. Don't use relative paths with ".." or like "foo/bar.cpp", else you'll get errors like "geninfo: WARNING: no data found for XXXX".
Don't include any header files on the command line to g++/gcc. Else you'll get "stamp mismatch with graph file" errors.
So, following should work when having multiple directories:
g++ --coverage -DDEBUG -g3 heyo.cpp /app/helper/blah.cpp /app/libfoo/foo.cpp -o program
./program
lcov --directory . --capture --output-file app.info
genhtml --output-directory cov_htmp app.info
Or, if you're in a Makefile that uses relative paths already, it's convenient to use:
g++ --coverage -DDEBUG -g3 $(abspath heyo.cpp helper/blah.cpp ../foo/bar/baz.cpp) -o program
To be able to keep your directory structure, you need to run gcov once inside each source file folder, but use the -o option to tell gcov where the data files are.
I think it should be like this:
gcov -o ../../Build/MyA *.cpp
I have a project with a similar source file structure, but I let the compiler dump object files etc into the source folders. I then run gcov multiple times from the root folder, once for each source file, but I specify the relative path of the source file and use the -o option to specify the relative folder like this:
gcov -o Source/MyA Source/MyA/aa.cpp
If you performed your product or application testing thoroughly and manually and spent lot of effort on it. If your objective is to get code coverage report using lcov and gcov but by mistake deleted gcno files. You can regenerate gcno files by recompiling the code but it will be generated with new timestamp and gcov reports error saying "stamp mismatch with graph file" and no code coverage report will be generated. This will result in all your testing effort getting wasted.
There is a shortcut to still generate the code coverage report. This is just a workaround and should not be relied upon all the time. Its recommended to preserve *.gcno files till your testing completes.
Note down your gcc version(gcc -v) and download its source code from one of the mirror sites
Eg - ftp://gd.tuwien.ac.at/gnu/sourceware/gcc/releases/gcc-4.4.6/gcc-4.4.6.tar.bz2
After extracting downloaded file, gcc the folder structure will be as follows
gcc-4.4.6
gcc-4.4.6/gcc
If you directly go inside gcc-4.4.6/gcc and try to do ./configure and compile(make) from there then you will encounter below problem
build/genmodes -h > tmp-modes.h
/bin/sh: build/genmodes: No such file or directory
Solution is do ./configure and make from gcc-4.4.6 and no errors will be shown related to genmodes. This will compile all modules including gcc. You may have to install mpfr and gmp modules which are needed by gcc if any error shown by ./configure
goto gcc-4.4.6/gcc/gcov.c and comment below lines and then recompile with above command
/* if (tag != bbg_stamp)
{
fnotice (stderr, "%s:stamp mismatch with graph file\n", da_file_name);
goto cleanup;
}*/
Example path of new gcov binary after compilation is gcc-4.4.6/host-x86_64-unknown-linux-gnu/gcc/gcov
Place this binary in /usr/bin and regenerate code coverage report with command as shown in below example
lcov --capture --directory ./ --output-file coverage.info ; genhtml coverage.info --output-directory /var/www/html/coverage
Now you should not get "stamp mismatch with graph file" error and you will get code coverage report properly

How do I tell lcov where to find my .cpp & .h files?

My source and build tree looks like this (see Makefile to put object files from source files different directories into a single, separate directory?) after a make (which builds and runs FooAndBarTests):
src
- Foo.cpp
- Bar.cpp
inc
- Foo.h
- Bar.h
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
- obj
- Foo.gcda
- Foo.gcno
- Bar.gcda
- Bar.gcno
- FooAndBarTests
UnitTest++
- libUnitTest++.a
- src
- ...
I can then produce .gcov files in the test directory by running gcov -o obj/ ../src/Foo.cpp and gcov -o obj/ ../src/Bar.cpp.
But if I run lcov -d obj/ -c -o FooAndBarTests.lcov I get:
Capturing coverage data from obj/
Found gcov version: 4.2.1
Scanning obj/ for .gcda files ...
Found 4 data files in obj/
Processing Foo.gcda
../src/Foo.cpp:cannot open source file
Processing FooTest.gcda
FooTest.cpp:cannot open source file
../inc/Foo.h:cannot open source file
../UnitTest++/src/Checks.h:cannot open source file
...
And then when I run genhtml FooAndBarTests.lcov I get:
Reading data file FooAndBarTests.lcov
Found 45 entries.
Found common filename prefix "/Users/dspitzer/FooAndBar/test"
Writing .css and .png files.
Generating output.
Processing file UnitTest++/src/Checks.h
genhtml: ERROR: cannot open /Users/dspitzer/FooAndBar/test/UnitTest++/src/Checks.h for reading!
How do I tell lcov and genhtml where the .cpp and .h files are?
Use the -b option to lcov. The -b option specifies code base.