Reputation: 14505
According to GCC online docs, gcov should be run with the current directory the same as that when you invoked the compiler. Let’s say I have the following source files layout:
-root
-source
-dir1
-file.cpp
-dir2
-file.cpp
I run gcc in directory “root” with according flags (namely -fprofile-arcs, -ftest-coverage, and –lgcov). Then I run the generated program. By default, .gcno, .gcda files are generated beside object files, which looks like:
-root
-source
-dir1
-file.cpp
-file.o
-file.gcno
-file.gcda
-dir2
-file.cpp
-file.o
-file.gcno
-file.gcda
It’s time to run gcov. The above online docs requires me to run gcov at the same directory “root” (where I run gcc to build the program). And .gcov coverage files will be generated at the current directory, which is also “root”. Since there are two files with the same name (both “file.cpp”), there will only be one file.cpp.gcov generated. The other is overwritten.
How gcov solves this problem? Does it support multiple files with the same name?
One workaround is to use –p
option (--preserve-paths
). But since it’ll create a super long file name in my case, I’d rather not to use it as a general solution.
I’d be glad to know if someone can share some backgrounds of why gcov has to be run at the same directory where gcc is invoked? Or maybe gcov can provide an option to define where those source files are located, to be more flexible?
Upvotes: 3
Views: 5644
Reputation: 3739
Since the -p option (--preserve-paths)
is gcov's workaround for this issue, they also created the -s
option(--source-prefix) to get rid of those pesky, long filenames.
-s directory
--source-prefix directory
A prefix for source file names to remove when generating the output coverage files. This option is useful when building in a separate directory, and the pathname to the source directory is not wanted when determining the output file names. Note that this prefix detection is applied before determining whether the source file is absolute.
I created a test case like you described above, and had success in eliminating parts of the path.
In your case, you could use this:
gcov -p -s source source/dir1/file.cpp
This means that instead of gcov creating a file named source#dir1#file.cpp.gcov it will create dir1#file.cpp.gcov in root.
I also tested(successfully) -s
with a longer path name to exclude such as:
gcov -p -s source/someDir source/someDir/dir1/file.cpp
So, instead of having a file named source#someDir#dir1#file.cpp.gcov, it will simply be dir1#file.cpp.gcov.
That way, you still have a unique file name, but only minimally so.
Also, this -s
option can also be used multiple times(I tested this out too), so you can also take out multiple ugly/long paths for files(e.g. /usr/include/c++/4.7.2/bits/ to just get a file named char_traits.h.gcov instead of usr#include#c++#4.7.2#bits#char_traits.h.gcov).
Upvotes: 4