Introduction
In GNU environments, C++ compilation is based on a tool called gcc. Within gcc, there are sub-tools such as g++, which is responsible for taking source code files (.c and .h) and automatically linking them to the necessary C++ libraries. Using the g++ command line, you can apply the following command options:
- -Wall: Enables additional warnings during the construction of objects that may be questionable but still conform to the C++ standard.
- **-o
:** Specifies the name of the executable file. By default, if this option is not defined, gcc creates an output object named a.out. - man g++ or info g++: Provides manuals and detailed information about additional commands and options available in g++.
To execute the output object generated by g++, use the command ./a.out in the terminal.
Qmake
As in any project, in Qt it is necessary to have a tool that builds executables through the compilation of source code and linking of any dependencies. This requires a precise specification of inputs, intermediate tools based on use cases for building, and an output target specification. One of the most commonly used utilities for this is make. Make reads all the details of a project from a special file called a makefile, which contains:
- Rules for building different types of files.
- A list of all the source files **and **headers needed for the project.
- The target, specifying which executable, library, or dependency will be built.
- Dependencies, listing which executables need to be rebuilt when a specific file has been modified.
Qt includes a derivative of make called qmake, which is very similar to make but enables the definition of a project file (.pro) that is easier to use within the Qt environment and framework.
https://github.com/CharlieHdzMx/QtProjects/blob/74228842151dd26094f96d52bb760f8c5b265221/FactDialog/mainApp.pro#L11C1-L21C42
The HEADER section specifies whether the project is an APP, LIB, or SUBDIR:
- For an APP, the output is an executable file.
- For a LIB, the output is a library, based on the specified output configuration.
- For a SUBDIR, it indicates that this is a parent .pro file, which will include sub-project .pro files with their own configurations.
Include headers
Qmake recognizes headers when the project file includes an INCLUDEPATH += dirName directive, allowing new headers or dependencies to be added to the project. Qt, like C++, supports three different ways of including a header in the source code:
- _#include
_: This first searches for the header sequentially in the **"include path,"** where the C++ standard implementation is typically located. - #include “headerName”: This attempts to find a header with the name headerName in the include paths and HEADERS specified in the project file.
- #include “seqPath/headerName”: This searches first in the specified seqPath for a header with the name headerName.
When there is a name conflict with headerName, qmake uses the first header it finds. If it cannot locate any headerName, qmake will fail to produce a new executable and will return an error for the project.
Main Functon Arguments
In C++ and Qt, the main function serves as the entry point for an application, whether it is a command-line tool or a graphical application (e.g., a widget-based app). For command-line applications specifically, arguments can be passed to the program when running the executable with additional parameters. The main function in both standard C++ and Qt applications typically defines two parameters:
- argc: The argument count, which represents the number of arguments passed from the command line.
- argv: The argument vector, which is an array of strings (char*) containing the actual arguments separated by spaces.
In Qt applications, it is essential to declare a QCoreApplication or QApplication object as early as possible in the main() function. This is important because it enables the event loop, allowing any events that affect or are of interest to the application to be processed promptly. The QCoreApplication class translates the console argument vector into a QStringList, making it easier to process these arguments using the QString API. In English
Reference
[1] Ezust, A., & Ezust, P. (2006). An Introduction to Design Patterns in C++ with Qt 4. Prentice Hall. ISBN-10: 0131879057, ISBN-13: 978-0131879058.