Up: README.md, Prev: Section 9, Next: Section 11
We've compiled a small editor so far. The program is not complicated yet, but if it grows bigger, it will be difficult to maintain. So, we should do the following now.
- We've had only one C source file and put everything in it. We need to divide it into several files.
- There are two compilers,
gccandglib-compile-resources. We should control them with one build tool.
When you divide C source file into several parts, each file should contain one thing. For example, our C source has two parts, one is for TfeTextView and the other is for application, including GtkApplication and GtkApplicationWindow. So, we divide it into:
tfetextview.handtfetextview.ctfeapplication.c
In tfeapplication.c, the function tfe_text_view_new is called to create a TfeTextView instance.
However, since this function is defined in tfetextview.c, using it in tfeapplication.c without a prior declaration will cause a compilation error.
To prevent this, the function must be declared.
In C, such declarations (along with variables and macros) are typically written in header files.
For example, by placing the declaration of tfe_text_view_new in tfetextview.h and including this header file in both tfeapplication.c and tfetextview.c, we can successfully avoid compilation errors.
In addition, we have some other files.
tfe.ui: a UI file.tfe.gresource.xml: a resource description file
Now we face another problem. The number of build steps has increased.
- Compiling the UI file
tfe.uiintoresources.c, usingtfe.gresource.xmlby resource compiler. - Compiling
tfeapplication.cintotfe.o(object file). - Compiling
tfetextview.cintotfetextview.o. - Compiling
resources.cintoresources.o. - Linking all the object files into application
tfe.
How can we manage the build process above? It is time to use Build tools to manage the steps.
Meson and Ninja are one of the most popular build systems. Many developers, includeing GTK developers, use them.
First, You need to create meson.build file.
project('tfe', 'c')
gtkdep = dependency('gtk4')
gnome=import('gnome')
resources = gnome.compile_resources('resources','tfe.gresource.xml')
sourcefiles=files('tfeapplication.c', 'tfetextview.c')
executable('tfe', sourcefiles, resources, dependencies: gtkdep, install: false)
- 1: The function
projectdefines things about the project. The first argument is the name of the project and the second is the programming language. - 3: The function
dependencydefines a dependency that is taken bypkg-config. We putgtk4as an argument. - 5: The function
importimports a module. In line 5, the gnome module is imported and assigned to the variablegnome. The gnome module provides helper tools to build GTK programs. - 6: The method
.compile_resourcesis from the gnome module. It compiles files to resources, as instructed by the xml file. In line 6, the resource filename isresources, which meansresources.candresources.h, and the xml file istfe.gresource.xml. This method generates C source file by default. - 8: Defines source files.
- 10: The executable function generates a target file by compiling the source files.
The first argument is the filename of the target, and the following arguments are the source files.
The last two arguments are provided as "key-value" pairs.
For example, the fourth argument consists of the key
dependenciesand the valuegtkdep, separated by a colon (:). This type of argument is called a keyword argument (or kwargs). The valuegtkdepis defined on line 3. The final argument specifies that this executable will not be installed to a system directory (such as/usr/local/bin). Instead, it is only built and kept within the build directory.
Now, build the project by running Meson and Ninja.
The whole programs are in src/tfe4 directory.
Change your current directory to src/tfe4 and type the following on your command line.
$ meson setup _build
$ ninja -C _build
Here, the meson command takes two arguments:
setup: The primary command for Meson. Although it is the default action, explicitly specifyingsetuphas been recommended since Meson version 0.64.0._build: The name of the build directory.
Once the build is complete, the executable file tfe will be generated inside the _build directory.
You can run it using the following command:
$ _build/tfe tfeapplication.c tfetextview.c
A window with two pages appears.
One is tfeapplication.c and the other is tfetextview.c.
This application has four buttons but their signal handlers are not implemented yet. So, you can just display the files. Even you can edit the files on the screen, the files will not be updated.
For further information, see The Meson Build system.
Up: README.md, Prev: Section 9, Next: Section 11