about
Bits Hello C++
09/02/2023
0
Bits: Hello C++
code, output, and build for C++ on Windows, macOS, and Linux
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.
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
// Cpp_Hello.cpp
#include <iostream>
int main() {
std::cout << "\n Hello C++ World" << "\n\n";
}
Output
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
> debug/Cpp_Hello
Hello C++ World
C:\github\JimFawcett\Bits\Cpp\Cpp_Hello\build
>
CMakeLists.txt
#---------------------------------------------------
# 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
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
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
3.0 References
Reference | Description |
---|---|
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 |