dead code detector tools for c/c++ in linux

Asked by srinivas

Hi all,
i need some dead code (unused variables and functions) detector tools tools in ubuntu 14.04.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
N1ck 7h0m4d4k15 (nicktux) said :
#1

Hello,

I'm quoting an answer, related to your query, from stackoverflow

[quote start]

You could use a code coverage analysis tool for this and look for unused spots in your code.

A popular tool for the gcc toolchain is gcov, together with the graphical frontend lcov (http://ltp.sourceforge.net/coverage/lcov.php).

If you use gcc, you can compile with gcov support, which is enabled by the '--coverage' flag. Next, run your application or run your test suite with this gcov enabled build.

Basically gcc will emit some extra files during compilation and the application will also emit some coverage data while running. You have to collect all of these (.gcdo and .gcda files). I'm not going in full detail here, but you probably need to set two environment variables to collect the coverage data in a sane way: GCOV_PREFIX and GCOV_PREFIX_STRIP...

After the run, you can put all the coverage data together and run it through the lcov toolsuite. Merging of all the coverage files from different test runs is also possible, albeit a bit involved.

Anyhow, you end up with a nice set of webpages showing some coverage information, pointing out the pieces of code that have no coverage and hence, were not used.

Off course, you need to double check if the portions of code are not used in any situation and a lot depends on how good your tests exercise the codebase. But at least, this will give an idea about possible dead-code candidates...

[quote end]

Source: http://stackoverflow.com/a/229121

Program lcov is exist in Ubuntu repositories, you can install it through Ubuntu Software Center of via terminal

    $ sudo apt-get install lcov

Regards
NickTh

P.S. Don't forget to mark the question as [SOLVED], when done.

Revision history for this message
srinivas (svr-0453) said :
#2

Thank you nickth for responding my question.
I have a doubt that how to analyse, unused variables, unused functions in my project with that output "index.html".

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

For unused variables, you can enable the flags

-Wunused or -Wunused-variable or -Wall

of the gcc compiler.

(There are more 'unused' flags including -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-function -Wunused-label -Wunused-result)

Can you help with this problem?

Provide an answer of your own, or ask srinivas for more information if necessary.

To post a message you must log in.