- C language
- Compile
- here
Explanation of split compilation method
I will explain the division compilation method of C language. Use gcc for the compiler. This is a split compilation method that can be used in practice. The split compilation method is described here by dividing it into the application source file, library header file, and library source file, and compiling them. , Link to generate an executable file.
Overview of split compilation method
I will explain the outline of the split compilation method.
Application source files
The application source file is the source file that contains the main function. The application runs from here.
In order to compile the application source files, the functions and structures used in the application must be present in the included header. ..
When compiling the source files of your application, you only need to resolve the names of functions, structs, etc., and you don't need to know the source files of other libraries. If you want to link, you will need to compile the source files of the library into object files.
Library header file
This is the header file of my own library. Structure definition, typedef statement, enums, constant macros, function macros , Function prototype declaration is done in the header file.
It is included in the source file of the application or the source file of the library.
Library source files
This is the source file of my own library. In the source file of the library, function definition is performed. Function prototype declarations are made in the header file.
In the library source file, include the library header file.
In order to compile the library source file, the functions and structures used in the library source file must be present in the included header.
When compiling the source files of a library, you only need to resolve the names of functions, structs, etc., and you don't need to know the source files of other libraries. If you want to link, you will need to compile the source files of the library into object files.
Split compilation procedure
This is the procedure for split compilation.
1. Compile the library source files to generate object files. All done in the library source files.
2. Compile the application source file to generate an object file.
3. Link all the object files generated above to generate an executable file.
The header file will be stored in a directory called "include". We will store the application source files and library source files in a directory called "src". In the header file, include guard is performed.
Split compilation sample
This is a sample of split compilation.
Application source files
The source file "myapp.c" for the application.
myapp.c
#include <stdio.h> #include "mylib1.h" #include "mylib2.h" int main (void) { mylib1_print (); mylib2_print (); }
Library header file
The library header files "mylib1.h" and "mylib2.h".
mylib1.h
#ifndef MYLIB1_H #define MYLIB1_H void mylib1_print (void); #endif
mylib2.h
#ifndef MYLIB2_H #define MYLIB2_H void mylib2_print (void); #endif
Library source files
The library source files "mylib1.c" and "mylib2.c".
mylib1.c
#include <stdio.h> #include "mylib1.h" void mylib1_print (void) { printf("mylib1_print\n"); }
mylib2.c
#include <stdio.h> #include "mylib2.h" void mylib2_print (void) { printf("mylib2_print\n"); }
Compile
Compile the application source file and the library file respectively. Specify the location of the header file with the option "-I".
gcc -Iinclude -c -o myapp.o src / myapp.c gcc -Iinclude -c -o mylib1.o src / mylib1.c gcc -Iinclude -c -o mylib2.o src / mylib2.c
When compiled, it becomes an object file. The object file is in machine language, but it is not an executable yet.
Link-Create executable file
Link to create an executable file. Symbols such as function names are tied to the entity.
gcc -o myapp myapp.o mylib1.o mylib2.o
Execution
Let's execute the executable file.
./myapp
This is the output result.
mylib1_print mylib2_print
You have now mastered the C language split compilation technique. It is the same even if the number of library files increases.
In this example, only the function prototype declaration is made in the header and the function definition is made in the source file, but the important thing is to understand exactly what should be defined in the header.
In the header file, structure definition, typedef statement, Enumeration, Constant macro, Function macro, declare the function as a prototype.
In the source file, function definition is performed.
How to avoid circular references of type
One problem with the above split compilation method is that it creates a circular reference of the type. Cross-references between libraries are a common occurrence in practical programming, and one way to solve this is with a technique called type declaration headers. You can reduce the reading between header files and resolve circular references.
Object-oriented C language
We also introduce the object-oriented C language as an application of split compilation.