The Hello World
Outputs 'Hello World!' in the console.
#include <iostream>
int main(int argc, char **argv)
{
std::cout << "Hello World!" << std::endl;
return 0;
}How to build
g++ hello.cc -o helloif you have clang instead:
clang++ hello.cc -o helloHow to run
./helloThe modern C++ hello world
C++ keeps evolving and new, major features are coming. Modules, for instance, offers faster compile time for big projects and drift away from classic header files. So, our modern hello world follows:
import <iostream>;
int main(int argc, char **argv)
{
std::cout << "Hello World!" << std::endl;
return 0;
}Modules are still considered experimental so you need two command lines to make it work using g++:
# precompile the module
g++ -fmodules-ts -std=c++23 -x c++-system-header iostream
# follow normal compilation path
g++ -fmodules-ts -std=c++23 hello-modern.cc -o hello-modernThe C compatibility
A lot of weird stuff we see in C++ are "due historical reasons". By that read: keep C++ code a compatible superset of C.
It's perfectly possible to write modern, safe C++ code nowadays, but is also possible to write it as unsafe as you want. The regular C++ developer wants freedom and flexibility, there already lots of safe languages out there, like java, go, python, you name it.
Noteworthy
Since the advent of multitasking, all that modern computer needs to work is an Operating System (or Control Program, if you ar that old) and a compiler (or an interpreted language, such as BASIC). Things are like that because the programs need to be compiled for that specific combination of hardware and OS.