GCC -fprofile-arcs -ftest-coverage編譯選項

gcc需要靜態注入目標程序編譯選項,在編譯鏈接的時候加入2個選項(-ftest-coverage -fprofile-arcs ),編譯結束之後會生成 .gcno 文件,而經過靜態注入的目標程序在“正常結束”後,會在運行目錄下產生.gcda數據文件,通過gcov工具就可產生覆蓋率數據結果。

  • -ftest-coverage

    Produce a notes file that the gcov code-coverage utility (see gcov—a Test Coverage Program) can use to show program coverage. Each source file’s note file is called auxname.gcno. Refer to the -fprofile-arcs option above for a description of auxname and instructions on how to generate test coverage data. Coverage data matches the source files more closely if you do not optimize.
    讓編譯器生成與源代碼同名的.gcno文件(note file),這種文件含有重建基本塊依賴圖和將源代碼關聯至基本塊的必要信息。

  • -fprofile-arcs

    Add code so that program flow arcs are instrumented. During execution the program records how many times each branch and call is executed and how many times it is taken or returns. When the compiled program exits it saves this data to a file called auxname.gcda for each source file. The data may be used for profile-directed optimizations (-fbranch-probabilities), or for test coverage analysis (-ftest-coverage). Each object file’s auxname is generated from the name of the output file, if explicitly specified and it is not the final executable, otherwise it is the basename of the source file. In both cases any suffix is removed (e.g. foo.gcda for input file dir/foo.c, ordir/foo.gcda for output file specified as -o dir/foo.o). See Cross-profiling.
    讓編譯器靜態注入對每個源代碼行關聯的計數器進行操作的代碼,並在鏈接階段鏈入經態度libgcov.a,其中包含在程序正常結束時生成*.gcda文件的邏輯;

可以參考:
https://blog.csdn.net/LU_ZHAO/article/details/104517587

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章