Lets get the Hello C++ out of the way!

I have been learning Modern C++ after 15 years in .NET Framework, Node.js and Python. Why Modern C++ and why now? So far it doesn't seem like a lot different than many modern languages on the market right now. I'd like to dive into many interesting new features, so let us get the obligatory Hello World program out of the way first.

// The 'import' in many languages
#include <iostream>

// Ok, at least I didn't know C++ functions could have default
// parameters. Also, it is good to see 'string' instead of 'char[]'
void say_hello(std::string world = "C++")
{
  std::cout << "Hello " << world << std::endl;
}

// The main function must return an 'int', but doesn't require
// to return anything
int main()
{
  // 'std' is the Standard namespace available in all programs
  say_hello("World"); // output: Hello World
  say_hello();        // output: Hello C++

  // Here's something funny: say_hello function must be
  // followed by the main function, otherwise it will
  // trigger a compile-type error, because the say_hello
  // function hasn't been declared yet.
}

What happens when we run this program

If you have a MinGW based setup on Windows or on Linux, you execute g++ your-program.cpp it will compile the program and create an executable for it. The latter part is also known as linking.

C++ compiling and linking

Unlike Node.js and Python, in compiled languages, you typically see two phases. The compiler turns source codes into objects. The third-party libraries are very often shared as objects. Then the relevant and required objects are combined into an executable, which is known as linking.

For a successful linking, if a 3rd-party library is used, while coding we need to ensure that we refer them using #include by the header files come with the package. Then we need to ensure that we use its object into linking, so that the executable can use it.

The code of this post can be found here. If you are looking for a slighly better build system, check out the next post: Static Library Linking with CMake and VS:MSVC & CLion:G++.