问题背景

1. gcov是与gcc和g++一起工作的一个覆盖率工具,一个简单的例子如下

$ gcc -fprofile-arcs -ftest-coverage tmp.c
$ a.out
$ gcov tmp.c
  90.00% of 10 source lines executed in file tmp.c
  Creating tmp.c.gcov.

per: here

2. 将编译和链接两个步骤分开,会提示错误信息 undefined reference to ‘_gcov_init’

$ gcc -c tmp.c -fprofile-arcs -ftest-coverage
$ gcc tmp.o -o tmp 

错误信息:

tmp.c:(...): undefined reference to '__gcov_init'

tmp.o:(...): undefined reference to '__gcov_merge_add'

这和gcov的工作原理有关了

原理简介 per: here

至于其他更普遍的 "undefined reference to" error 可以顾名思义,也可以参考一个简单的例子 per: here

解决方案

在链接的时候你可以选择 a) 或 b) :

a) add -fprofile-arcs (success)

b) add -lgcov (fail, and prompt a message 'gcov: invalid option -- 'g' ')

 

$ gcc tmp.o -o tmp -fprofile-arcs

per: here

 

(已补完/已解答)

-lgcov未成功的原因?

-lgcov选项的含义?

回复来自Nathan本人,本来并没有期望9年之后邮件地址还能有效,就连他自己都说gcov都变了很多……

gcov has changed a lot since then.

邮件下半截我向Nathan询问了 -lgcov 失败的原因,这个似乎并不是 gcov command 的一个选项,Nathan回复如下:

Correct. -lgcov is an option to the compiler. IIRC -ftest-coverage -fprofile-arcs is the right invokation now.

 

意思是,-lgcov 不是 gcov 的选项,而是编译器的,在 gcc/cc/g++ 的 man-db 中看到 -lgcov 的寥寥两处,简单说明是在做覆盖率分析的代码进行 link 时候使用的

不过既然问题已经解决,-lgcov 也没有更多线索可循,也就放弃探索了

By the way, IIRC is short for If I Recall Correctly (如果我没有记错的话)

 posted on 2013-01-06 10:25  turtle_fly  阅读(6075)  评论(0编辑  收藏  举报