about
Bits Hello C++
09/02/2023
0
Bits Repo Code Bits Repo Docs

Bits: Hello C++

code, output, and build for C++ on Windows, macOS, and Linux

These pages support comparison of fragments of code in several different languages, much as you might compare a sentence of English with one in Spanish to help you learn Spanish or English.

Synopsis:

This page demonstrates use of Visual Studio Code to build and execute a simple C++ "Hello World" program, showing its build process and launch file. The purpose is to show how to get started quickly.
  • Companion pages for Rust, C#, Python, and JavaScript are accessible from links in the left panel.
  • Navigation through example levels for the same language is effected with the "Bits Pages" button in the left panel, or "Next" and "Prev" buttons in the menus or by using the "N" and "P" keys.
  • Links in the left panel point to major sections of this page.
Note:
Most page links in this site refer to specific points in a target page, usually the top. For all of the code bits here, language links return to the last scroll position. This supports quickly moving between similar code for each of the languages covered, maintaining context for comparisons.

Source Code

Code below is a fragment taken from Bits Repository. You can download the repo containing code for all Bits by opening the "Code" dropdown in that page.
// Cpp_Hello.cpp

#include <iostream>

int main() {
    std::cout << "\n  Hello C++ World" << "\n\n";
}          

Output

This is output from the C++ source code above, running in Visual Studio Code. The output is retrieved in an embedded terminal window. VS Code runs on Windows, Linux, and macOS.
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
> debug/Cpp_Hello

  Hello C++ World

C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
>          

CMakeLists.txt

When using VS Code, I build all C++ code with CMake. CMake works with many different compilers on Windows, Linux, and macOS. This window shows the CMakeLists file, a makefile on steroids.
#---------------------------------------------------
# HelloCMake - Demonstrate building C++ with CMake
#   - Non-hierarchal version
#---------------------------------------------------

#---------------------------------------------------
project(HelloCMake)
#---------------------------------------------------
#   CppHello dir
#   -- CMakeLists.txt (this file)
#   -- src dir
#      -- Cpp_Hello.cpp
#   -- build directory
#      -- Debug directory
#         -- Cpp_Hello.exe
#         -- ...
#      -- Cpp_Hello.dir directory
#         -- Debug directory
#            -- Cpp_Hello.obj

#---------------------------------------------------

set(CMake_CXX_STANDARD 23)

#---------------------------------------------------
# build HelloCMake.obj in folder build/Cpp_Hello.dir/debug
#---------------------------------------------------
add_executable(Cpp_Hello src/Bits_Hello.cpp)

#---------------------------------------------------
# build HelloCMakeLib.lib in folder build/debug
#---------------------------------------------------
#add_library(HelloCMakeLib STATIC libs/hello_lib/hello_lib.cpp)

#---------------------------------------------------
# link HelloCMake.obj and HelloCMakeLib.lib to
# create HelloCMake.exe in folder  build/debug
#---------------------------------------------------
#target_link_libraries(HelloCMake HelloCMakeLib)

#---------------------------------------------------
# Build process with CMake version 3.18.2
#---------------------------------------------------
#   1. add source files to project directories
#   2. add CMakeLists.txt (this file)
#   3. mkdir build                 - this puts intermediate
#   4. cd build                      files in build directory
#   5. cmake ..                    - create cmake config files
#   6. cmake --build . [--config Debug | --config Release]
#   7. "./Debug/HelloCmake.exe"    - runs executable
#   8. Notes:
#      - you can change any of the source files then:
#          cmake --build . --config Debug
#          "./Debug/HelloCmake.exe
#      - delete contents of build directory to clean
#          cmake will regenerate
#   9. Note:
#      - you can substitute Release for Debug
#        in contents of 8.
#---------------------------------------------------

Build

To build a project with CMake you use two commands, cmake .. to build CMake's working files, and cmake --build . to build the project. You should create a "build" directory and issue cmake commands there. That avoids burying your source code amidst many temporary cmake files.
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
> cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22621.
-- The C compiler identification is MSVC 19.34.31942.0
-- The CXX compiler identification is MSVC 19.34.31942.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/github/JimFawcett/Bits/Cpp/Cpp_Hello/build
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
> cmake --build .
MSBuild version 17.4.1+9a89d02ff for .NET Framework
  Checking Build System
  Building Custom Rule C:/github/JimFawcett/
  Bits/Cpp/Cpp_Hello/CMakeLists.txt
  Bits_Hello.cpp
  Cpp_Hello.vcxproj -> C:\github\JimFawcett\
  Bits\Cpp\Cpp_Hello\build\Debug\Cpp_Hello.e
  xe
  Building Custom Rule C:/github/JimFawcett/
  Bits/Cpp/Cpp_Hello/CMakeLists.txt
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
>          

Create Project

The terminal session below shows how to create a project for C++ with CMake infrastructure. See tooling information here: Bits_Tooling.
C:\github\JimFawcett\test
> mkdir Cpp_Hello

    Directory: C:\github\JimFawcett\test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2023   3:24 PM                Cpp_Hello

C:\github\JimFawcett\test
> cd Cpp_Hello
C:\github\JimFawcett\test\Cpp_Hello

--- copy CMakeLists.txt here ---

> mkdir src

    Directory: C:\github\JimFawcett\test\Cpp_Hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2023   3:25 PM                src

--- copy source code into src directory ---

C:\github\JimFawcett\test\Cpp_Hello
> mkdir build

    Directory: C:\github\JimFawcett\test\Cpp_Hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2023   3:25 PM                build

C:\github\JimFawcett\test\Cpp_Hello
> dir

    Directory: C:\github\JimFawcett\test\Cpp_Hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2023   3:25 PM                build
d-----         2/23/2023   3:26 PM                src
-a----         2/23/2023   7:29 AM           2322 CMakeLists.txt

C:\github\JimFawcett\test\Cpp_Hello
> cd build
C:\github\JimFawcett\test\Cpp_Hello\build
> cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22621.
-- The C compiler identification is MSVC 19.34.31942.0
-- The CXX compiler identification is MSVC 19.34.31942.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/github/JimFawcett/Test/Cpp_Hello/build
C:\github\JimFawcett\test\Cpp_Hello\build
>

2.0 VS Code View

The code for this demo is available in github.com/JimFawcett/Bits. If you click on the Code dropdown you can clone the repository of all code for these demos to your local drive. Then, it is easy to bring up any example, in any of the languages, in VS Code. Here, we do that for Cpp\Cpp_Hello. Figure 1. VS Code IDE Figure 2. Launch.JSON Figure 3. C++ Plugins

3.0 References

ReferenceDescription
C++ Story E-book with thirteen chapters covering most of intermediate C++
C++ Bites Relatively short feature discussions (currently incomplete)
Bits Tooling Tool chains, VS Code