Compiling C program with Visual Studio Code using Windows
Compilation is a process of converting the source code
into machine or object code. It is done with the help of the compiler. The
compiler checks the source code for the errors, and if no error arises, then it
generates the machine code. The C compilation process can be divided into four
steps, i.e., Pre-processing, Compiling, Assembling, and Linking.
1. Preprocessor - The source code is first passed through the
preprocessor, where the code is expanded. After expansion, the expanded code is
passed to the compiler.
2. Compiler - The compiler converts the expanded code into
3. Assembler - The assembly code is converted into machine code
by using an assembler.
3. Linker- All the programs written in C use library functions. These library functions are pre-compiled. The main work of the linker is to combine the machine code of library files with the machine code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file.
Visual Studio Code extensions let you add languages, debuggers, and tools to support your development workflow. Visual Studio Code allows extensions to define a language configuration for any programming language. This file controls basic editing features such as comment toggling, bracket matching/surrounding, and region folding. I had an unfortunate encounter once, my C program wasn't compiling on my Visual Studio code even after installing all C/C++ extensions needed. I then realized that extensions were only half of what was needed. The GNU compiler collection needed to be installed.
GNU Compiler installation
is a toolchain that compiles code, links it with any library dependencies,
converts that code to assembly, and then prepares executable files. When you run
GCC on a source code file, it first uses a preprocessor to include header files
and discard comments. Next, it tokenizes the code, expands macros, detects any
compile-time issues, then prepares it for compilation. It is then sent to the
compiler, which creates syntax trees of the program’s objects and control flow
and uses those to generate assembly code. The assembler then converts this code
into the binary executable format of the system. Finally, the linker includes
references to any external libraries as needed. The finished product is then
executable on the target system.
The GNU compiler can be gotten by installing MinGW-w64 using msys2 according to your system’s bit size whether 32 or 64. Follow the instructions for installation and run the msys2 installer afterward. On the installer follow the steps in the msys2 documentation to install GCC.
These are the steps:
1. use Pacman -syu to update base packages and package database.picture
2. Pacman -su to update the remaining base packages.picture
Afterward, open the MSYS MinGW w64 or MinGW w32 terminal to install the GNU compiler
64-bit system use:
Pacman -S mingw-w64-x86_64-gcc
Pacman -S mingw-w64-1686-gcc
After installation, check the GCC version on CMD by typing GCC --version.
If an error message comes up saying that GCC is not recognized as an internal or external command, operable, or batch file, then the system environment
path needs to be changed. Locate the MSYS folder, open the MinGW w64 or MinGW
w32 folder and locate the bin folder where GCC is installed and copy the path (C:\msys64\mingw64\bin).
Search for the system environment variable at the start menu. Insert path copied into environment variable path and click ok.
check the version again, everything should be fine.
- Grace Effiong
- Mar, 25 2022