C++ 使用 gcov 和 lcov 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4844188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Using gcov and lcov problem?
提问by cpp_noob
I am using ubuntu 10.1, g++ compiler.
我正在使用 ubuntu 10.1,g++ 编译器。
I trying to use gcov and lcov for my C++ project. I manage to compile the gcov:
我试图在我的 C++ 项目中使用 gcov 和 lcov。我设法编译 gcov:
g++ -fprofile-arcs -ftest-coverage main.cpp user.cpp game.cpp
There is no error or warning message. Next I try to run gcov:
没有错误或警告消息。接下来我尝试运行 gcov:
gcov main.cpp user.cpp game.cpp
Also fine. I also try to run my program:
也不错。我也尝试运行我的程序:
./a.out
and run gcov again, my main, user and game.cpp shows some percentage now. I want to capture the data, I type this in terminal:
并再次运行 gcov,我的主要用户和 game.cpp 现在显示了一些百分比。我想捕获数据,我在终端中输入:
lcov --directory /home/projects/Game1/ -c -o application.info
But it gives me this:
但它给了我这个:
Capturing coverage data from /home/projects/Game1/
geninfo: ERROR: cannot read /home/projects/Game1/!
I search all over the web, read lcov documentation, I cant find the answers. Anyone can help me?
我在网上搜索,阅读 lcov 文档,我找不到答案。任何人都可以帮助我吗?
In addition, I also could not open the main.gcda file.(I tried open using text editor, it says some character encoding problem, quite alot: UTF-8, Western (ISO-8859-1), Western (ISO-8859-11) etc, but still cant open and read the file.
另外,我也打不开main.gcda文件。(我试过用文本编辑器打开,它说一些字符编码问题,很多:UTF-8、Western (ISO-8859-1)、Western (ISO-8859) -11) 等,但仍然无法打开和读取文件。
Please help me.. anyone??
请帮帮我..有人吗??
EDIT:
编辑:
I admit, its my mistake (i am terribly sorry, "home/Projects/Game1" with capital "P".) After verifying the path, I got this new error:
我承认,这是我的错误(非常抱歉,“home/Projects/Game1”带有大写“P”。)在验证路径后,我得到了这个新错误:
geninfo: ERROR: /home/Projects/Game1/main.gcno: reached unexpected end of file
回答by Blaise
Be sure to include -gflag (debug information): -g -fprofile-arcs -ftest-coverage
确保包含-g标志(调试信息):-g -fprofile-arcs -ftest-coverage
While working with lcovI found that it is better to use absolute paths instead of relative paths. You can try to use lcov to capture initial zero coverage date with -i, --initial switch.
在使用lcov 时,我发现最好使用绝对路径而不是相对路径。您可以尝试使用 lcov 使用 -i, --initial 开关捕获初始零覆盖日期。
Here is an example of my way of achieving zerocounters
这是我实现零计数器的方式的示例
$ lcov --zerocounters --directory myFullPath
$ lcov --capture --initial --directory myFullPath --output-file myOutputFile
Then run your program and then capture the coverage data:
然后运行你的程序,然后捕获覆盖数据:
$ lcov --no-checksum --directory myFullPath --capture --output-file myOutputFile
Finaly lcov enables you to generate html report:
最后 lcov 使您能够生成 html 报告:
$ lcov/genhtml --highlight --legend --output-directory myOutPutHTMLdirectory myOutputFile
Hope this helps you.
希望这对你有帮助。
回答by PierreBdR
I had exactly the same problem. And it turned out that if the library code changes while you had already ran the coverage tests, sometimes it gets confused. The only way to get out of it is to remove the file(s) causing the error and recompile.
我遇到了完全相同的问题。事实证明,如果在您已经运行覆盖测试时库代码发生变化,有时它会变得混乱。摆脱它的唯一方法是删除导致错误的文件并重新编译。
回答by Aakash Anuj
Compile the code using :
使用以下代码编译代码:
lcov --directory . --output-file app.info
Then use the following to make the html pages.
然后使用以下内容制作 html 页面。
genhtml app.info