Mac OS X Lion 和 XCode 4 / llvm-g++-4.2 没有代码覆盖率
其他人报告无法 使用 XCode 4 生成代码覆盖率,但我发现我不仅无法在 XCode 4 中执行此操作,甚至无法从命令行使用简单的玩具程序执行此操作。我按照此处和这里,这导致我创建了这个 cov.c 文件:
#include <stdio.h>
int main (void) {
int i;
for (i = 1; i < 10; i++) {
if (i % 3 == 0)
printf("%d is divisible by 3\n", i);
if (i % 11 == 0)
printf("%d is divisible by 11\n", i);
}
return 0;
}
然后使用以下命令尝试生成代码覆盖率:
g++ -c -g -O0 --coverage -o $PWD/obj/cov.o $PWD/cov.c
g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o
$PWD/bin/cov
唉,obj 目录中不存在 cov.gcno 文件。事实上,此后我拥有的唯一文件是: 冠状病毒c obj/cov.o bin/cov
此外,如果我输入 nm bin/cov,我会得到以下信息:
0000000100001048 S _NXArgc
0000000100001050 S _NXArgv
0000000100001060 S ___progname
0000000100000000 A __mh_execute_header
0000000100001058 S _environ
U _exit
0000000100000e40 T _main
U _printf
0000000100001000 s _pvars
U dyld_stub_binder
0000000100000e00 T start
这表明 libgcov.a 从未链接到。如果我替换
g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o
为:
g++ -g -O0 --coverage -o $PWD/bin/cov -lgcov $PWD/obj/*.o
我会得到完全相同的结果。
更多信息:
g++ --version
产生:“i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (基于Apple Inc. build 5658)(LLVM build 2336.1.00)”- 我也尝试过使用gcc(即llvm-gcc)。
Other people have reported not being able to generate code coverage with XCode 4, but I find not only can I not do it from within XCode 4, I can't do it even with a simple toy program from the command line. I followed the examples given here and here, which led me to create this cov.c file:
#include <stdio.h>
int main (void) {
int i;
for (i = 1; i < 10; i++) {
if (i % 3 == 0)
printf("%d is divisible by 3\n", i);
if (i % 11 == 0)
printf("%d is divisible by 11\n", i);
}
return 0;
}
I then used the following commands in an attempt to generate code coverage:
g++ -c -g -O0 --coverage -o $PWD/obj/cov.o $PWD/cov.c
g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o
$PWD/bin/cov
Alas, no cov.gcno file exists in the obj directory. In fact, the only files I have after this are:
cov.c
obj/cov.o
bin/cov
Furthermore, if I type nm bin/cov
, I get the following:
0000000100001048 S _NXArgc
0000000100001050 S _NXArgv
0000000100001060 S ___progname
0000000100000000 A __mh_execute_header
0000000100001058 S _environ
U _exit
0000000100000e40 T _main
U _printf
0000000100001000 s _pvars
U dyld_stub_binder
0000000100000e00 T start
This suggests that libgcov.a was never linked in. If I replace
g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o
with:
g++ -g -O0 --coverage -o $PWD/bin/cov -lgcov $PWD/obj/*.o
I get the exact same results.
More information:
g++ --version
yields: "i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1
(Based on Apple Inc. build 5658) (LLVM build 2336.1.00)"- I've also tried using gcc (which is llvm-gcc).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够使用这个答案的帮助找到这个问题的答案。基本上,我将覆盖命令更改为使用
clang
而不是g++
(因为示例文件是纯 C,所以我使用clang
而不是 < code>clang++,我已经验证它对于 C++ 文件工作得很好)。从那里,我能够使用 lcov 生成类似于我在 Java/cobertura 中看到的输出。I was able to figure out an answer to this question using help from this answer. Basically, I changed my coverage commands to use
clang
instead ofg++
(because the example file was pure C, I went withclang
instead ofclang++
, which I've verified works just fine with C++ files). From there, I was able to use lcov to generate output similar to what I'm used to seeing from Java/cobertura.正如您所发现的,在 Lion 上,
g++
是llvm-g++
的别名。要调用“真正的”gcc,请使用 gcc-4.2 或 g++-4.2:On Lion
g++
is an alias forllvm-g++
, as you have discovered. To invoke "real" gcc, usegcc-4.2
org++-4.2
: