Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate SLOC GCC C/C++ Linux

Tags:

c++

c

linux

gcc

We have a quite large (280 binaries) software project under Linux and currently it has a very dispersed code structure - that means one can't [work out] what code from the source tree is valid (builds to deployable binaries) and what is deprecated. But the Makefiles are good. We need to calculate C/C++ SLOC for entire project.

Here's a question - can I find out SLOC GCC has compiled? Or maybe I can gain this information from binary (debug info probably)? Or maybe I can find out what source files was the binary compiled from and use this info to calculate SLOC?

Thanks Bogdan

like image 841
Bogdan Avatar asked Oct 24 '25 06:10

Bogdan


1 Answers

It depends on what you mean by SLOC that GCC has compiled. If you mean, track the source files from your project that GCC used, then you'd probably use the dependency tracking options which lists source files and headers. That's -M and various related options. Beware of including system-provided headers. A technique I sometimes use is to replace the standard C compiler with an appropriate variation - for example, to ensure a 64-bit compilation, I use 'CC="gcc -m64"' to guarantee the when the C compiler is used, it will compile in 64-bit mode. Obviously, with a list of files, you can use wc to calculate the number of lines. You use 'sort -u' to eliminate duplicated headers.

One obvious gotcha is if you find that everything is included with relative path names - then you have to work out more carefully where each file is.

If you have some other definition of SLOC, then you will need to specify what you have in mind. Sometimes, people are looking for non-blank, non-comment SLOC, for example - but you still need the list of source files, which I think the -M options will help you determine.

like image 56
Jonathan Leffler Avatar answered Oct 26 '25 19:10

Jonathan Leffler