본문 바로가기

Programming/C++ Basic

[Basic C++] 03 - How is C++ code formed : Code Structure, Compiling and Linking

In this post, I am going to talk about the basic structure of C++ code structure with compiling/linking processes. Since I only knew Python back in graduate school, it took me a lot of time to understand how to code in C++. It was because not only the different syntax but also C++ is a real programming language that you have to understand how your computer works fundamentally.


Table of Contents

1. Basic code structure
2. Compiling and Linking


1. Basic code structure : header file / main function

Header files are pre-defined files that contain lines of code, so you can use them conveniently without re-writing them again. Including a header file is very simple as seen in Figure 1. You just write "#include <the name of header file>". 

 

Figure 1 C++ code structure

 

When you compile your source file, MSVC reads the header files first and copy/paste all of the pre-defined code into your source file. That's all they are for. In order to understand this, you can write the below source file and additional header file and compile it.

 

Figure 2 How to add a header file in your project

I have made the below header file named "example_1.h" under the header files folder. 

 

Code 1 - Example header file

// example_1.h
// This is an example header file.
}

And then erase the right curly bracket in your source file and replace it with the example_1.h.

 

Code 2 - Example header file in the main source file

#include <iostream>

int main()
{
    std::cout << "Hello World !" << std::endl;
    std::cin.get();
#include "example_1.h"

Figure 3 shows the result of the source file. There no compilation failure here. This is a good example to tell you how header files work. They simply contain pre-defined codes that you need to use in your source file and MSVC just copies and pastes the codes in your source file at the beginning of the compilation.

 

Figure 3 Result of example code

Header files can also refer to other header files like a chain of reference.

 

2. Compilation and Linking

2.1 Compilation

Python is an interpreted language and C++ is a compiled language. Like I already said before, a compilation is a process that MSVC transforms your code into binary code that your computer can understand. Therefore it is platform-dependent. Once compiled in Windows can not be run in Mac.

 

On the other hand, Python code goes through an interpreter that can run your code immediately without compilation. Therefore it is platform-independent. It takes a bit more time than C++ to execute your code but you can easily modify and test your code.

 

C++ compiler creates an "object file (.obj)" from your source file (.cpp) as a result of a compilation. However, it is not executable yet. Linker appears after a compilation to "link" the object file and runtime library files to create an executable file (.exe).

 

Figure 4 Concept of compilation and linking (1)

 

If you compile the below source file,  the MSVC compiler will create an object file in your project directory.

 

Code 3 - Main source file

#include <iostream>

void return_sin(double degree); // Signiture of the function

int main()
{
    double degree;
    std::cout << "What degree ? ";
    std::cin >> degree;
    return_sin(degree);

    std::cin.get();
}

Figure 5 Main Object file

You can't do anything here since a compilation is just a process that transforms your code into a binary file so your computer can read it.

2.2 Linking

Linking comes right after compilation to create an executable file of your program by linking all of the source files and libraries in your project. Let suppose we have two different source files in the project. One is the main and another is a source file that contains a function.

 

First, you need to write a function signate of your function. A function signature consists of the function prototype. It tells you the general information of your function; mainly its name and parameters. Once you type the signature of your function, MSVC knows that you are going to add the function inside of the current source file.

 

However, it does not tell you whether it actually exists or not. MSVC just believes that you've already created the function outside of the current source file. Therefore, if you have not created the function somewhere in your project, the build operation will fail in the middle.

 

Code 4- Additional source file

// Additional source file 01
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>

void return_sin(double degree)
{
	double radian = degree * M_PI/180;
	std::cout <<"sin("<<degree<<") is = "<< std::sin(radian) << std::endl;
}

Here is the additional source file that contains the function called "return_sin". This function accepts "double degree" value and calculates radian value out of it. Finally, it shows you the sin value at the given radian value.

 

I used "cmath" library to call "sin function". In addition, in order to use "PI" value, you have to define _USE_MATHY_DEFINES before including header files. This code activates the pre-defined math variables like PI and exponential.

 

Figure 5 shows the result of example codes 3 and 4. The project was built perfectly with no errors and the function worked just fine. This example shows how C++ Linker wors perfectly. The build process of your project goes through two steps. 

 

Figure 5 Result of example code 3 and 4

The first step is a compilation. In this stage, MSVC compiler compiles your source files but none of them are related during this stage. Even if you write a bunch of function signatures to refer to other source files, MSVC does not really care about them. It just believes that you have already written those functions somewhere in the project. As a result of the compilation, you get an object file for an each source file.

 

The second step is linking. In this stage, MSVC linker gathers all of the objects files (complied source files) / library files and puts them into one executable file. During the process, the linker checks the references and validity of your code so your project runs with no errors.

 

Figure 6 Schematic explanation of compilation and linking


REFERENCES

(1) University of Hawaii, ICS 111

http://www2.hawaii.edu/~takebaya/ics111/process_of_programming/process_of_programming.html

(2) Cherno, Youtube, https://www.youtube.com/watch?v=H4s55GgAg0I, https://www.youtube.com/watch?v=3tIqpEmWMLI&list=PLCgLdtL_uIxzxVKnnAWY-5iyd8csy7wwP&index=12&t=0s

(3) cpluscplus, http://www.cplusplus.com/