Using gcov with shared objects used by executable

2019-09-09 16:04发布

问题:

I am trying to generate coverage report for my project. the .gcno files are generated as expected for the shared object code files, but when I run the executable (which uses the shared object generated), .so is also present in same location as executable but .gcda files for shared object doesnot get generated whereas .gcda files for executable code is generated as expected. Is there something wrong in approach? PLEASE ADVISE

回答1:

Its possible to get coverage for shared libarary. i tried simple hello application profiling with gcov with shared library concept ,im able to get code coverage. let take two files hello.c and extlib.c

First compile extlib.c

#include <stdio.h>

extern void print(const char* p, ...);

void print(const char* p, ...) {
    printf("%s World!\n", p);

}

gcc -shared -fPIC extlib.c -o libext.so -ftest-coverage -fprofile-arcs

here will get extlib.gcno libextlib.so

next link it to main hello program //hello.c extern void print(const char*, ...);

int main() {
    print("Hello");
}

gcc hello.c -L./ -lextlib -o test -ftest-coverage -fprofile-arcs

after this hello.gcno file. execute ./test

After execution will get extlib.gcda and hello.gcda

using gcov *.c can check coverage.

so my suggestion try some simple file in your project individually compiling it with gcov profiling.



标签: gcov