使用gcov是否可以覆盖共享库的代码? [英] Is it possible code coverage of a shared library using gcov?

查看:146
本文介绍了使用gcov是否可以覆盖共享库的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试测试使用OpenCV共享库的可执行文件.当使用gcov知道覆盖了哪些代码行时,我仅获得有关库的.cpp文件和.hpp的信息.没有显示有关该库的.cpp文件的信息.

I try to test an executable which uses OpenCV shared library. When using gcov to know what code lines were covered I only get info about my .cpp files and .hpp of the library. No info is shown about .cpp files of the library.

我编译并与-pg --coverage标志链接.

I compiled and linked with -pg --coverage flags.

推荐答案

是的,gcov可以提供有关共享库的覆盖率信息.如果我正确地记住了使它在项目中起作用的问题,则可能是在动态库的链接上未包含--coverage标志.这是我可以创建的最小示例.

Yes, gcov can give coverage information about a shared library. If I remember correctly from the problems I had getting this to work on my project, you're probably not including the --coverage flag on the linking of the dynamic library. Here's the smallest example I could create.

Makefile:

CXXFLAGS += --coverage
LDFLAGS += --coverage

myexec: myexec.cpp libmylib.so

libmylib.so: mylib.o
    gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o

mylib.o: CXXFLAGS += -fPIC

myexec.cpp:

myexec.cpp:

#include "mylib.h"

int main(int argc, char** argv)
{
    return is_even(argc);
}

mylib.h

#ifndef MYLIB_H
#define MYLIB_H

int is_even(int num);

#endif

mylib.cpp

mylib.cpp

#include "mylib.h"

int is_even(int num)
{
    if (num % 2)
        return false;
    else
        return true;
}

make的输出(因此您可以确切地看到构建是什么):

Output of make (so you can see exactly what the build was):

g++ --coverage -fPIC   -c -o mylib.o mylib.cpp
gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o
g++ --coverage  --coverage  myexec.cpp libmylib.so   -o myexec

我使用LD_LIBRARY_PATH="." ./myexec a运行可执行文件,然后运行gcov mylib.cpp.这是mylib.cpp.gcov的内容:

I ran the executable using LD_LIBRARY_PATH="." ./myexec a, and then ran gcov mylib.cpp. Here's the contents of mylib.cpp.gcov:

    -:    0:Source:mylib.cpp
    -:    0:Graph:mylib.gcno
    -:    0:Data:mylib.gcda
    -:    0:Runs:1
    -:    0:Programs:1
    -:    1:#include "mylib.h"
    -:    2:
    1:    3:int is_even(int num)
    -:    4:{
    1:    5:    if (num % 2)
#####:    6:        return false;
    -:    7:    else
    1:    8:        return true;
    -:    9:}

这篇关于使用gcov是否可以覆盖共享库的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆