{"id":153,"date":"2015-01-07T09:37:03","date_gmt":"2015-01-07T07:37:03","guid":{"rendered":"http:\/\/gronlier.fr\/blog\/?p=153"},"modified":"2020-03-30T12:43:44","modified_gmt":"2020-03-30T09:43:44","slug":"adding-code-coverage-to-your-c-project","status":"publish","type":"post","link":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/","title":{"rendered":"Adding Code Coverage to your C++ project"},"content":{"rendered":"<p>For that example, I'll use one of my GitHub project <a href=\"https:\/\/github.com\/ticapix\/arduino-toolbox\" target=\"_blank\" rel=\"noopener noreferrer\">arduino-toolbox<\/a>, <a href=\"https:\/\/travis-ci.org\" target=\"_blank\" rel=\"noopener noreferrer\">Travis-CI<\/a> and <a href=\"https:\/\/coveralls.io\/\">Coveralls<\/a> but the commands lines will be exactly the same for any other setup.<\/p>\n<p>Since the project is using Cmake, the following commands could go inside an\u00a0<a href=\"http:\/\/www.cmake.org\/cmake\/help\/v3.0\/command\/add_custom_target.html\" target=\"_blank\" rel=\"noopener noreferrer\"><span class=\"lang:default highlight:0 decode:true crayon-inline\">add_custom_target<\/span>\u00a0 <\/a>rule in CMakeLists.txt<\/p>\n<p><!--more--><\/p>\n<h2>Adding C++ coverage flag<\/h2>\n<p>Add\u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline\">--coverage<\/span>\u00a0 to your <span class=\"lang:default highlight:0 decode:true crayon-inline \">CFLAGS<\/span> or <span class=\"lang:default highlight:0 decode:true crayon-inline \">CXXFLAGS<\/span>.<\/p>\n<p>With CMake for C++, it gives you:<\/p>\n<pre class=\"lang:default decode:true\">set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -g -O0\") # debug, no optimisation\r\nset(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} --coverage\") # enabling coverage\r\n<\/pre>\n<p>Replace\u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline \">CMAKE_CXX_FLAGS<\/span>\u00a0 by\u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline \">CMAKE_C_FLAGS<\/span>\u00a0 for a C project.<\/p>\n<p>Nothing more is needed. From the GNU GCC man:<\/p>\n<blockquote><p><b>--coverage<\/b><\/p>\n<p>This option is used to compile and link code instrumented for coverage analysis. The option is a synonym for <b>-fprofile-arcs -ftest-coverage<\/b> (when compiling) and <b>-lgcov<\/b> (when linking).<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>The Travis-CI configuration<\/h2>\n<p>At the root of your git repository, in <span class=\"lang:default highlight:0 decode:true crayon-inline \">.travis.yml<\/span>\u00a0:<\/p>\n<pre class=\"lang:yaml decode:true\">install:\r\n- cd ${TRAVIS_BUILD_DIR}\r\n# install latest LCOV (1.9 was failing for me) [1]\r\n- wget http:\/\/ftp.de.debian.org\/debian\/pool\/main\/l\/lcov\/lcov_1.11.orig.tar.gz\r\n- tar xf lcov_1.11.orig.tar.gz\r\n- sudo make -C lcov-1.11\/ install\r\n# install lcov to coveralls conversion + upload tool\r\n- gem install coveralls-lcov\r\n\r\nbefore_script: (optional: for paranoiacs only)\r\n- cd ${TRAVIS_BUILD_DIR}\r\n- lcov --directory . --zerocounters\r\n\r\nscript:\r\n- cd ${TRAVIS_BUILD_DIR}\r\n- &lt;your command that compiles&gt; # will generate *.gcno files\r\n- &lt;your command that runs&gt; # will generate *.gcda files\r\n\r\nafter_success:\r\n- cd ${TRAVIS_BUILD_DIR}\r\n- lcov --directory . --capture --output-file coverage.info # capture coverage info\r\n- lcov --remove coverage.info 'tests\/*' '\/usr\/*' --output-file coverage.info # filter out system and test code\r\n- lcov --list coverage.info # debug before upload\r\n- coveralls-lcov --repo-token ${COVERALLS_TOKEN} coverage.info # uploads to coveralls\r\n<\/pre>\n<p>My full\u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline\">.travis.yml<\/span>\u00a0 with support for GTest\/GMock and GCC C++11 enable compiler is <a href=\"https:\/\/github.com\/ticapix\/arduino-toolbox\/blob\/master\/.travis.yml\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>The value of the environment variable <span class=\"lang:default highlight:0 decode:true crayon-inline \">${COVERALLS_TOKEN}<\/span>\u00a0 is set in the settings page of your Travis project (https:\/\/travis-ci.org\/&lt;github-user&gt;\/&lt;project-name&gt;\/settings) under the 'Environment Variables' section.<\/p>\n<h3>Notes<\/h3>\n<p>[1]: this should be not needed when travis VM image will update to Ubuntu Utopia.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Update: 13\/01\/2016<\/p>\n<p>As mentioned in the comment and <a href=\"https:\/\/github.com\/ticapix\/arduino-toolbox\/issues\/1\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>, you SHOULD not specify your repo-token on the command line.<\/p>\n<div class='skype-share' data-href='https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/' data-lang='auto' data-style='large' data-source='WordPress' ><\/div><div style='clear:both;padding-bottom:10px;'><\/div>\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=ticapix\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For that example, I'll use one of my GitHub project arduino-toolbox, Travis-CI and Coveralls but the commands lines will be exactly the same for any other setup. Since the project is using Cmake, the following commands could go inside an\u00a0add_custom_target\u00a0 rule in CMakeLists.txt<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[25,26],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adding Code Coverage to your C++ project - Bits and Bytes<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding Code Coverage to your C++ project - Bits and Bytes\" \/>\n<meta property=\"og:description\" content=\"For that example, I&#039;ll use one of my GitHub project arduino-toolbox, Travis-CI and Coveralls but the commands lines will be exactly the same for any other setup. Since the project is using Cmake, the following commands could go inside an\u00a0add_custom_target\u00a0 rule in CMakeLists.txt\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/\" \/>\n<meta property=\"og:site_name\" content=\"Bits and Bytes\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-07T07:37:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-30T09:43:44+00:00\" \/>\n<meta name=\"author\" content=\"ticapix\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ticapix\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/\",\"url\":\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/\",\"name\":\"Adding Code Coverage to your C++ project - Bits and Bytes\",\"isPartOf\":{\"@id\":\"https:\/\/gronlier.fr\/blog\/#website\"},\"datePublished\":\"2015-01-07T07:37:03+00:00\",\"dateModified\":\"2020-03-30T09:43:44+00:00\",\"author\":{\"@id\":\"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/9abd8d0260a4d942d7069de75446e2bc\"},\"breadcrumb\":{\"@id\":\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/gronlier.fr\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adding Code Coverage to your C++ project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/gronlier.fr\/blog\/#website\",\"url\":\"https:\/\/gronlier.fr\/blog\/\",\"name\":\"Bits and Bytes\",\"description\":\"As the default template said: &quot;Just another WordPress site&quot;\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/gronlier.fr\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/9abd8d0260a4d942d7069de75446e2bc\",\"name\":\"ticapix\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/94bba7228ec3665afea02078abebfa94?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/94bba7228ec3665afea02078abebfa94?s=96&d=monsterid&r=g\",\"caption\":\"ticapix\"},\"url\":\"https:\/\/gronlier.fr\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adding Code Coverage to your C++ project - Bits and Bytes","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/","og_locale":"en_US","og_type":"article","og_title":"Adding Code Coverage to your C++ project - Bits and Bytes","og_description":"For that example, I'll use one of my GitHub project arduino-toolbox, Travis-CI and Coveralls but the commands lines will be exactly the same for any other setup. Since the project is using Cmake, the following commands could go inside an\u00a0add_custom_target\u00a0 rule in CMakeLists.txt","og_url":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/","og_site_name":"Bits and Bytes","article_published_time":"2015-01-07T07:37:03+00:00","article_modified_time":"2020-03-30T09:43:44+00:00","author":"ticapix","twitter_card":"summary_large_image","twitter_misc":{"Written by":"ticapix","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/","url":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/","name":"Adding Code Coverage to your C++ project - Bits and Bytes","isPartOf":{"@id":"https:\/\/gronlier.fr\/blog\/#website"},"datePublished":"2015-01-07T07:37:03+00:00","dateModified":"2020-03-30T09:43:44+00:00","author":{"@id":"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/9abd8d0260a4d942d7069de75446e2bc"},"breadcrumb":{"@id":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gronlier.fr\/blog\/2015\/01\/adding-code-coverage-to-your-c-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gronlier.fr\/blog\/"},{"@type":"ListItem","position":2,"name":"Adding Code Coverage to your C++ project"}]},{"@type":"WebSite","@id":"https:\/\/gronlier.fr\/blog\/#website","url":"https:\/\/gronlier.fr\/blog\/","name":"Bits and Bytes","description":"As the default template said: &quot;Just another WordPress site&quot;","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gronlier.fr\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/9abd8d0260a4d942d7069de75446e2bc","name":"ticapix","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gronlier.fr\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/94bba7228ec3665afea02078abebfa94?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/94bba7228ec3665afea02078abebfa94?s=96&d=monsterid&r=g","caption":"ticapix"},"url":"https:\/\/gronlier.fr\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":11,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/posts\/153\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gronlier.fr\/blog\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}