Domanda

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.

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

È stato utile?

Soluzione

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:

#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

#include "mylib.h"

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

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

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:}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top