What is a cppcheck Command in Linux?
The Linux operating system provides a cppcheck command to analyze a source code file for bugs and errors. It is a command line tool that analyzes a C or C++ file to find errors and bugs that a compiler might miss. It can manage multiple preprocessor commands, macros, etc. It is a versatile, efficient, and reliable tool that is capable of checking the inline assembly code, compiler extensions, nonstandard code, etc. It includes various static checks which might not be covered in the compilers themselves. These static analysis checks, including memory leak checks, bounds checking, dead code elimination, automatic variable checking, resource leak checking, etc., are performed at the source code level without really executing the code. All in all, the purpose is to find the real errors in the code.
How to Enable the cppcheck Command in Linux?
The Linux operating system does not install the libraries, packages, and commands automatically. The user has to manually install the commands and libraries in Linux to use them. Hence, the cppcheck package needs to be installed in the Linux operating system so that the cppcheck command can be used properly. To do that, we will use the apt command:
This will install the cppcheck package and allows you to use the cppcheck command. To start the installation process, you may have to provide the root password:
Given the password so that the installation process can begin. Once the installation of the cppcheck package is done, you will get the following output:
Syntax of cppcheck Command in Linux
The cppcheck command is used to analyze a C or C++ file, so it will take the file name to be analyzed. Here is the syntax of the cppcheck command:
The cppcheck is the name of the command that will analyze the file for bugs and errors. The -option refers to one of the options available for the cppcheck command, like -E, -U, all, style, warning, performance, etc. To acquire the list of all the options, the –help option is used with the cppcheck command. Use the command below and display all the options for the cppcheck command:
This will return the whole list of options with their description that can be used with the cppcheck command. See the list below:
And finally, the file-name/directory represents either the name of the file that needs to be analyzed or the directory that contains the file. If the specified file name is not provided and the directory is provided, then the cppcheck command will look for all the source files and recursively check them. Let’s say we have a source code file named “ccode.c,” and it is located in the Downloads folder. Let us examine it with the cppcheck command:
Here, the -check-config flag is used to check the configuration of the cppcheck. Here is the output of the above command:
The normal code analysis is disabled by the –check-config flag, so it will give the proper analysis. Rather, it will show you the errors and bugs in the code, which you can see in the screenshot above.
Print the Preprocessor Output on the Standard Terminal
If you need to see the code with its output on the standard stdout and do not want to do anything, then you can use the -E flag with the cppcheck command; the -E flag enables the cppcheck command to display the preprocessor output on stdout. Here is the executable command:
This will give you whatever the ccode.c file has and does not perform any action on the code. See the output below:
As you can see, the -E flag has displayed the code stored in the ccode.c file and did not do anything with it.
Enable All Additional Checks
The standard checks of cppcheck might be just checking inline code or compiler extension. To enable all other checks, the cppcheck has a -all flag. This enables all the checks for the cppcheck command so that the whole program can be checked for additional checks, including portability, style, warning, etc. Let us execute the –enable flag with the cppcheck command with the “all” extension and enable all additional checks. See the command given in the below screenshot:
This will enable all the additional checks in the “ccode.c” file. Here is the output we get when we execute the above command:
Since there are some errors in the code, the cppcheck command has returned the errors and bugs found in the code. All the options of the cppcheck command are used similarly. They all follow the same format but return a different result. Use any option or flag by following the same syntax, and you will get the desired result.
Conclusion
This short manual is a quick overview of the cppcheck command in the Linux operating system. The cppcheck command in Linux operating system is a command line tool that is used to examine a source code file to find the bugs and errors which may be missed out by the compiler itself. It is a user-friendly and efficient command line tool that allows the user to find the problem in code. We used some flags with the cppcheck command to test the working of the cppcheck command and see how it works. If you follow these instructions and sample examples, you will be able to use any option or flag associated with the cppcheck command.