我正在尝试为在 Debian Linux 上使用 clang 编译的小型 C 程序生成代码覆盖率文件。这是我所做的:
neuron@debian:~/temp$ ls
main.c test.c test.h
neuron@debian:~/temp$ clang *.c
neuron@debian:~/temp$ ./a.out
0
这完全符合预期,我可以编译和运行东西。现在尝试启用覆盖。
neuron@debian:~/temp$ clang --coverage *.c
/usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
尝试包含用于链接的库。
neuron@debian:~/temp$ clang --coverage -lprofile_rt *.c
/usr/bin/ld: cannot find -lprofile_rt
clang: error: linker command failed with exit code 1 (use -v to see invocation)
寻找图书馆:
neuron@debian:~/temp$ find / -name \*profile_rt\* 2>/dev/null
/usr/lib/llvm-3.0/lib/libprofile_rt.so
/usr/lib/llvm-3.0/lib/libprofile_rt.a
neuron@debian:~/temp$ clang --coverage -lprofile_rt -L/usr/lib/llvm-3.0/lib *.c
/usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是最后一个命令的更详细的输出:http://pastie.org/8468331 .我关心的是:
- 链接器使用大量 gcc 库进行链接(尽管这可能是 llvm 没有自己的 binunitls 的结果);
- 正在
/usr/bin/../lib/libprofile_rt.a
中搜索分析库,而不是我提供的路径。
如果我们将参数传递给链接器,输出是相同的:
neuron@debian:~/temp$ clang --coverage -Wl,-L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
/usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我做错了什么?
最佳答案
尝试更改链接行的顺序
clang --coverage -lprofile_rt -L/usr/lib/llvm-3.0/lib *.c
到
clang --coverage -L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
好吧,似乎链接器由于某种原因没有得到你的 -L。也许试试
clang --coverage -Wl,L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
关于clang 中的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881928/