Posted: . At: 12:24 PM. This was 8 years ago. Post ID: 9248
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Count the actual lines of code in your C or C++ project.

This is how to actually count the lines of code in your C project on Linux. Some people advocate using wc -l, but that would not suit our purposes. The cloc utility is able to do this and more.

Firstly, install this utility.

sudo apt install cloc

Then run the utility in a source folder.

jason@jason-desktop:~/Documents/ipinfo/src$ cloc .
       2 text files.
       2 unique files.                              
       0 files ignored.
 
http://cloc.sourceforge.net v 1.60  T=0.01 s (138.5 files/s, 11358.2 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C/C++ Header                     1             13             10             66
C                                1             14              8             53
-------------------------------------------------------------------------------
SUM:                             2             27             18            119
-------------------------------------------------------------------------------

This utility shows the number of files, the lines of actual code, and the comments. Very useful for keeping track of how big your project is. It also shows a summary row with the totals at the bottom.

Use the –strip-comments=.strip parameter to write copies of the files with all comments and blank lines edited out.

jason@jason-desktop:~/Documents/ipinfo/src$ cloc . --strip-comments=.strip
       2 text files.
       2 unique files.                              
Wrote info.h..strip
Wrote ip.c..strip
       0 files ignored.
 
http://cloc.sourceforge.net v 1.60  T=0.01 s (286.3 files/s, 22190.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                                1              0              0             66
C/C++ Header                     1             13             10             66
-------------------------------------------------------------------------------
SUM:                             2             13             10            132
-------------------------------------------------------------------------------

Very useful for making a simplified C source file, but having comments in the file is very useful when someone else has to take on your code.

1 thought on “Count the actual lines of code in your C or C++ project.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.