#2 The starting Line – First Program
Ex01
// Ex01.cpp - The starting line
import std;
int main()
{
int answer {1};
std::println("Bits and Bytes program # {}.", answer);
return 0;
}
Code Breakdown
This code uses the new C++ 23 standard.
In C++23, the import std;
statement is used to import the standard library as a module. This is part of the C++20 modules feature, which aims to improve code modularity, compile times, and dependency management.
Modules: Modules are a new feature introduced in C++20 and further improved in C++23. They provide a way to break down code into smaller, self-contained units that can be compiled separately. This can reduce compilation times and improve the overall structure of your code.
When compiled there is an object file that is created called std.obj along with your program code. This allows the linker to combine the two and generate the final exe.
In order to tell the compiler at the command line that you want to use C++23 you need to use the switch /std:c++latest, but this is not enough to complete the job. there are other parts to the command line statement that need to be entered.
build.bat
@echo off
mkdir ..\build
pushd ..\build
cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx" "%VCToolsInstallDir%\modules\std.compat.ixx" ../src/Ex01.cpp
cl /EHsc /std:c++latest ../build/Ex01.obj ../build/std.obj /Fe:Ex01.exe
popd
“%VCToolsInstallDir%\modules\std.ixx”
This tells the compiler to include the standard library module in the compile time process.
The std.compat
module is a part of the C++ standard library’s efforts to maintain backward compatibility with older code while adopting new modular features introduced in C++20 and beyond. It essentially acts as a bridge to ensure that existing code relying on traditional header files can still function correctly in a modular environment.
Therefore we add the “%VCToolsInstallDir%\modules\std.compat.ixx” as well.
The program entry
The entry point of this program is main(). However, that is not were the entire thing starts. Windows requires a ton of code for handling processes and other windowie suff..
Before main()
-
Operating System Initialization:
-
When you run your program, the operating system loads it into memory. It sets up the process, allocates memory, and initializes system resources.
-
-
Runtime Library Initialization:
-
The C++ runtime library takes over, handling various tasks such as setting up global variables, initializing the heap, and preparing the environment for your program.
-
-
Static Initializations:
-
Global and static objects are initialized. If you have any global or static objects with constructors, their constructors are called.
-
-
Entry Point:
-
In Windows, the default entry point for a console application is typically
_mainCRTStartup
(part of the C runtime library), which eventually calls yourmain()
function. -
For GUI applications, the entry point is usually
WinMain
.
-
Data Types
Data is a critical part of the program and in order to define the correct characteristic about the data we use types. The first type we have encountered in this program is INT, short for integer. This is a number but more specifically a whole number.
int main() has the int as the first part of the line. This is the return type of the function. This tells the compiler and that there will be a value that is given to the caller at the end of the function execution.