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
>