Generally, when someone refers to a processor in computing, they mean some sort of silicon chip that performs computations. The P in CPU, GPU, APU, NPU, and many other forms of logic chips, stands for “Processing”, i.e. Central Processing Unit. Each of them is a specific type of processor, optimised for its specific function. A preprocessor, however, doesn’t have anything to do with physical processors like the CPU or GPU. Instead, it’s a programming term, specifically relating to compiled languages.
What is a compiler?
A compiler is a piece of software that compiles a specific programming language into machine code. All programming languages need to be converted into machine code, as the human-readable instructions that make up usable programming languages aren’t directly executable by the computer. Some are designed to be interpreted on the fly when the code is being run. This approach is very flexible making it easy to make minor changes. Many other programming languages, however, need to be compiled to be able to be executed. In small programs, this doesn’t necessarily take long. With larger programs though, compilation can take minutes or even hours.
So why would you want to use a language that needs compiling? Well, it’s harder to reverse engineer. Interpreted programming languages often have their code distributed as is, making it easy to “steal”. For companies wanting to protect their intellectual property, compiling inserts a complex and unreliable step of reverse engineering to see the actual code. Compiled software also tends to be smaller, as it has been optimised for execution. This isn’t really a problem with small scripts, but when dealing with large software, hundreds of megabytes, or even gigabytes in size, this can have a noticeable effect.
So what does a preprocessor do?
A preprocessor is a utility that is run over code before it is compiled. Typically, a compiler will automatically call the preprocessor as it starts, however, they can also be run separately. The preprocessor’s job is to set up the code exactly as intended. It primarily does that by performing search and replace functions.
In C, for example, it is standard practice to use a number of standard libraries. These libraries define a series of functions that provide standard functionality. To import these libraries the “#include” keyword is used followed by a library name. The preprocessor searches through the code for statements like “#include” and replaces them. In the case of “#include” the preprocessor inserts the entire contents of the specified library.
This allows you to keep the actually written code easily readable while also making use of many powerful and preexisting tools. It saves you from having to reinvent the wheel for every application or having to paste some or all of the library into the code base.
Another potential use of a preprocessor is to strip or add certain functionality depending on the intent of the build. For example, if you’re wanting to test a new build, it can be really useful to include a bunch of debugging steps. Not only are these not needed in release builds, but they actually make the compiled software larger. A preprocessor can check compiler arguments and determine if certain functionality needs to be included or ignored. Similar to this, a preprocessor will typically strip comments out before the compiler, as these shouldn’t be included in a build.
Conclusion
A preprocessor is a tool run by a compiler to manipulate the code before it actually gets compiled. While the preprocessor can be called separately, it is typically called as part of the compilation process. The preprocessor looks for certain defined strings and replaces them with standard content. For comments, there is no replacement, but for statements that include external libraries, the preprocessor replaces the statement with the content of the actual library. The preprocessor can also take compile-time arguments to gate certain functionality. This is often used to include or exclude debug functionality from test or release builds.
Did this help? Let us know!