Because this makes a lot of work, I'd suggest that you don't do this. But anyway, here it goes:
You have three "sub-questions":
How to create a.out
in another directory?
The target directory has to exist. Then you use the option -o
on the compile and link command:
gcc -fprofile-arcs -ftest-coverage 1.c -o /home/user/john/a.out
You can even change the name of the executable if you like. This is quite common. ;-)
How to create 1.gcno
in another directory?
This file needs to be moved because it is generated aside the source file. On un*x (linux) you commonly use:
mv 1.gcno /home/user/john
Make sure that the target directory exists before.
How to create 1.gcda
in another directory?
Straight from the manual:
For example, if the object file /user/build/foo.o was built with -fprofile-arcs, the final executable will try to create the data file /user/build/foo.gcda when running on the target system. This will fail if the corresponding directory does not exist and it is unable to create it. This can be overcome by, for example, setting the environment as ‘GCOV_PREFIX=/target/run’ and ‘GCOV_PREFIX_STRIP=1’. Such a setting will name the data file /target/run/build/foo.gcda.
Let's assume that the build directory is /home/user/jane/test
. The GCOV_PREFIX_STRIP variable will remove as many directories from the build path as given. We need 4: "home", "user", "jane", and "test". The GCOV_PREFIX variable will then put the given path in front of the remaining:
GCOV_PREFIX=/home/user/john GCOV_PREFIX_STRIP=4 /home/user/john/a.out
Now, if you try to generated the annotated source file 1.c.gcov
you will find that it will be created aside of 1.c
. You need to move this file to your target directory, too, like the .gcno file. Please read the documentation of gcov
for more options. Unfortunately there seems to be no option to relocate the output and gcov
seems to not use GCOV_PREFIX and GCOV_PREFIX_STRIP.