Skip to content

Latest commit

 

History

History
354 lines (298 loc) · 19.7 KB

README.md

File metadata and controls

354 lines (298 loc) · 19.7 KB

C++ Examples

C++ project Directory examples\ contains ISO C++ code examples coming from various websites - mostly from the C++ project.
It also includes build scripts (Bash scripts, batch files, Make scripts) for experimenting with C++ on a Windows machine.

The code examples presented below can be built/run with the following command line tools:

Build tool Build file Parent file Environment(s)
cmd.exe build.bat   Windows only
make.exe Makefile Makefile.inc Any a)
sh.exe build.sh   Any a)
a) Here "Any" means "tested on Windows, Cygwin, MSYS2 and UNIX".
 

hello Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.sh
|   CMakeLists.txt
|   Doxyfile
|   Makefile
\---src
    \---main
        +---cpp
        |       main.cpp
        \---resources
                hello.png
                hello.txt

Batch file build.bat generates the hello.exe executable using one of the options -bcc, -clang, -gcc, -icx, -msvc (default) or occ :

> build -clang -verbose clean compile
Delete directory "build"
Toolset: Clang/GNU Make, Project: hello
Generate configuration files into directory "build"
Generate executable "hello.exe"

In the same way command make.exe reads its configuration from file Makefile and generates the hello.exe executable using variable TOOLSET, e.g. TOOLSET=clang (respectively bcc, gcc, icx, msvc or occ) :

> make TOOLSET=bcc clean build
"C:/opt/msys64/usr/bin/rm.exe" -rf "build"
"c:/opt/BCC-7.30-32bit/bin/bcc32c.exe"  -I "src" -q -w  -o build/hello.exe src/main.cpp -lq
src/main.cpp:
 
> make TOOLSET=clang clean build
C:/opt/msys64/usr/bin/rm.exe -rf "build"
"C:/opt/LLVM-16.0.6//bin/clang.exe"  --std=c++17 -O2 -Wall -Wno-unused-variable  -o build/Release/hello.exe src/main.cpp
 
> make TOOLSET=icx clean build
"C:/opt/msys64/usr/bin/rm.exe" -rf "build"
"C:/Program Files (x86)/Intel/oneAPI//compiler/latest/windows/bin/icx.exe"  -nologo -Qstd=c++17 -O2 -Wall -Wno-unused-variable  -o build/Release/hello.exe src/main.cpp -link -libpath:"C:/Program Files (x86)/Intel/oneAPI//compiler/latest/windows/compiler/lib/intel64"
 
> make TOOLSET=occ clean build
"C:/opt/msys64/usr/bin/rm.exe" -rf "build"
"C:/opt/orangec/bin/occ.exe"  --nologo -std=c++14  -o build/hello.exe src/main/cpp/main.cpp

call-by-copy Example

This example comes from stackoverflow post What is object slicing?; it has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.sh
|   CMakeLists.txt
|   Doxyfile
|   Makefile
\---src
    \---main
        \---cpp
                Main.cpp

Batch file build.bat generates the call-by-copy.exe executable using one of the options bcc, -clang, -gcc, -icx or -msvc :

> build -msvc -verbose clean compile
Delete directory "build"
Toolset: MSVC/MSBuild, Project: call-by-copy
Configuration: Release, Platform: x64
Generate configuration files into directory "build"
Generate executable "call-by-copy.exe"

In the same way command make.exe reads its configuration from file Makefile¨ and generates the call-by-copy.exe executable using variable TOOLSET, e.g. TOOLSET=msvc (or bcc, clang, gcc, icx) :

> make TOOLSET=msvc clean build
C:/opt/msys64/usr/bin/rm.exe -rf "build"
"%MSVC_HOME%/bin\Hostx64/x64/cl.exe"  -nologo -std:c++17 -EHsc -I"%MSVC_HOME%/include" -I"%WINSDK_HOME%/include/10.0.22000.0/ucrt" -I"%WINSDK_HOME%/include/10.0.22000.0/um"  -Fe"build/Release/call-by-copy.exe" src/main/cpp/Main.cpp -link -libpath:"%MSVC_HOME%/lib/x64" -libpath:"%WINSDK_HOME%/lib/10.0.22000.0/ucrt/x64" -libpath:"%WINSDK_HOME%/lib/10.0.22000.0/um/x64" Main.cpp

class-dispatching Example

This example has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   build.bat
|   build.sh
|   CMakeLists.txt
|   Doxyfile
|   Makefile
\---src
    \---main
        \---cpp
                Main.cpp

Command build.bat generates the class-dispatching.exe executable using one of the options -bcc, -clang, -gcc, -icx or -msvc (default).

> build -msvc -verbose clean run
Delete directory "build"
Toolset: MSVC/MSBuild, Project: class-dispatching
Configuration: Release, Platform: x64
Generate configuration files into directory "build"
Generate executable "class-dispatching.exe"
Execute "build\Release\class-dispatching.exe"
Base Dervied1foo 10
Base Derived2foo 20

func-destructuringExample

This example 1 comes from Fekirs's blog post "Destructure a C++ function"; it has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   CMakeLists.txt
\---src
    \---main
        \---cpp
                main.cpp

move-constructor Example

This example comes from INVIVOO post "A la redécouverte du C++ : && et std::mov". It has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   CMakeLists.txt
|   Makefile
\---src
    \---main
        \---cpp
                Main.cpp

Command build.bat generates the move-constructor.exe executable using one of the options -bcc, -clang, -gcc, -icx or -msvc :

> build -clang -verbose clean compile
Delete directory "build"
Toolset: Clang/GNU Make, Project: move-constructor
Generate configuration files into directory "build"
Generate executable "move-constructor.exe"

In the same way command make.exe reads the hand-written Makefile and invokes one of five C++ compilers to generate executable move-constructor.exe using variable TOOLSET, e.g. TOOLSET=clang (or gcc, icx, msvc, occ) :

> make TOOLSET=clang clean build
C:/opt/msys64/usr/bin/rm.exe -rf "build"
"C:/opt/LLVM-16.0.6//bin/clang.exe"  --std=c++17 -O2 -Wall -Wno-unused-variable  -o build/Release/move-constructor.exe src/main/cpp/Main.cpp

tuple-iterators Example

This example has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   build.bat
|   build.sh
|   CMakeLists.txt
|   Doxyfile
|   Makefile
\---src
    \---main
        \---cpp
                Main.cpp

Batch file build.bat generates the tuple-iterators.exe executable using one of the options bcc, -clang, -gcc, -icx or -msvc (default) :

> build -gcc -verbose clean compile
Delete directory "build"
Toolset: GCC/GNU Make, Project: tuple-iterators
Generate configuration files into directory "build"
Generate executable "tuple-iterators.exe"

In the same way command make.exe reads the hand-written Makefile and invokes one of five C++ compilers to generate executable tuple-iterators.exe using variable TOOLSET, e.g. TOOLSET=gcc (or bcc, clang, icx, msvc) :

> make TOOLSET=gcc clean build
C:/opt/msys64/usr/bin/rm.exe -rf "build"
"C:/opt/msys64/mingw64/bin/g++.exe"  --std=c++17 -O2 -Wall -Wno-unused-variable  -o build/Release/tuple-iterators.exe src/main/cpp/Main.cpp

visitor-pattern Example

This example has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   build.bat
|   build.sh
|   CMakeLists.txt
|   Doxyfile
|   Makefile
\---src
    \---main
        \---cpp
                Main.cpp

Batch files build.bat-verbose run generates and executes the C++ program build\Release\visitor-pattern.exe :

> build -verbose run
Toolset: MSVC/MSBuild, Project: visitor-pattern
Configuration: Release, Platform: x64
Generate configuration files into directory "build"
Generate executable "visitor-pattern.exe"
Execute "build\Release\visitor-pattern.exe"
do Up on This
do Up on That
do Up on TheOther
do Down on This
do Down on That
do Down on TheOther

Using build option -gcc we get the following output (same with option -clang, -icx or -occ) :

> build -verbose -gcc clean run
Delete directory "build"
Toolset: GCC/GNU Make, Project: visitor-pattern
Generate configuration files into directory "build"
Generate executable "visitor-pattern.exe"
Execute "build\visitor-pattern.exe"
do Up on This
do Up on That
do Up on TheOther
do Down on This
do Down on That
do Down on TheOther

Footnotes

[1] Support for static_assert

> cd
G:\examples\func-destructuring
 
> build -debug -clang clean run
[build] Options    : _CXX_STD=c++17 _TOOLSET=clang _VERBOSE=0
[build] Subcommands: _CLEAN=1 _COMPILE=1 _DOC=0 _DUMP=0 _LINT=0 _RUN=1
[build] Variables  : "BCC_HOME=C:\opt\BCC-10.2"
[build] Variables  : "CMAKE_HOME=C:\opt\cmake"
[build] Variables  : "DOXYGEN_HOME=C:\opt\doxygen"
[build] Variables  : "GIT_HOME=C:\opt\Git"
[build] Variables  : "LLVM_HOME=C:\opt\LLVM-15.0.7" (clang)
[build] Variables  : "MSVS_HOME=X:"
[build] Variables  : "MSVS_CMAKE_HOME=X:\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake"
[build] Variables  : "MSVS_MSBUILD_HOME=X:\MSBuild\Current"
[build] Variables  : "MSYS_HOME=C:\opt\msys64" (gcc)
[build] Variables  : "ONEAPI_ROOT=C:\Program Files (x86)\Intel\oneAPI" (icx)
[build] Variables  : "ORANGEC_HOME=C:\opt\orangec" (occ)
[build] rmdir /s /q "G:\examples\func-destructuring\build"
[build] Current directory is: "G:\examples\func-destructuring\build"
[build] "C:\opt\cmake\bin\cmake.exe" -G "Unix Makefiles" ..
-- The CXX compiler identification is Clang 15.0.7 with GNU-like command-line
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/opt/LLVM-15.0.7/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.5s)
-- Generating done (0.0s)
-- Build files have been written to: G:/examples/func-destructuring/build
[build] "C:\opt\msys64\usr\bin\make.exe"
[ 50%] Building CXX object CMakeFiles/func-destructuring.dir/src/main/cpp/main.cpp.obj
In file included from G:/examples/func-destructuring/src/main/cpp/main.cpp:3:
In file included from X:\VC\Tools\MSVC\14.39.33519\include\tuple:8:
X:\VC\Tools\MSVC\14.39.33519\include\yvals_core.h:892:1: error: static assertion failed: error STL1000: Unexpected compiler version, expected Clang 16.0.0 or
      newer.
_EMIT_STL_ERROR(STL1000, "Unexpected compiler version, expected Clang 16.0.0 or newer.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
X:\VC\Tools\MSVC\14.39.33519\include\yvals_core.h:516:44: note: expanded from macro '_EMIT_STL_ERROR'
#define _EMIT_STL_ERROR(NUMBER, MESSAGE)   static_assert(false, "error " #NUMBER ": " MESSAGE)
                                           ^             ~~~~~
1 error generated.
make[2]: *** [CMakeFiles/func-destructuring.dir/build.make:76: CMakeFiles/func-destructuring.dir/src/main/cpp/main.cpp.obj] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/func-destructuring.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Error: Failed to generate executable "func-destructuring.exe"

mics/March 2025