问题
I have a C++ library (.so) which is opened using dlopen() by another application. But I need to find the code coverage of this library while run within the application using gcov. Is it possible? If yes, how can it be done?
If not, how can the issue be resolved?
回答1:
Firstly have your compiled your C++ library with the --coverage
flag? I've never actually used a '.so' library with gcov before, so I'm not sure it would work anyway.
Secondly could you arrange a test version of your application to not use dlopen()
, but instead be linked to a static library(.a) version of your library and still make the usual calls?
回答2:
Yes. Coverage of shared library loaded by dlopen
can be generated.
1) compile shared library with flags -fprofile-arcs -ftest-coverage
2) compile program that using dlopen
with flags -fprofile-arc -ftest-coverage
3) lcov to generate .info
file
lcov --capture --rc lcov_branch_coverage=1 --directory path/to/.gcda --output-file coverage.info
4) generate html
genhtml coverage.info --branch-coverage --output-directory out
来源:https://stackoverflow.com/questions/6368124/how-to-find-the-coverage-of-a-library-opened-using-dlopen