From 2845b020aee7ab4349a0f696d3ede2377b2d3884 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 7 Jan 2022 11:26:00 -0500 Subject: [PATCH] Update README instructions + Fix incorrect library name for `algorithms/trees/BST` example + Update root CMakeLists.txt for major directories to set binary path + Add instructions to install CMake LTS with pip --- cpp/CMakeLists.txt | 2 + cpp/README.md | 75 ++++++++++++++++--- cpp/algorithms/CMakeLists.txt | 2 + cpp/algorithms/graphs/CMakeLists.txt | 2 + cpp/algorithms/sorting/CMakeLists.txt | 2 + cpp/algorithms/sorting/bubble/CMakeLists.txt | 2 - cpp/algorithms/sorting/bucket/CMakeLists.txt | 2 - cpp/algorithms/sorting/count/CMakeLists.txt | 2 - cpp/algorithms/sorting/heap/CMakeLists.txt | 2 - .../sorting/insertion/CMakeLists.txt | 2 - cpp/algorithms/sorting/merge/CMakeLists.txt | 1 - cpp/algorithms/sorting/quick/CMakeLists.txt | 2 - cpp/algorithms/sorting/radix/CMakeLists.txt | 2 - .../sorting/selection/CMakeLists.txt | 2 - cpp/algorithms/trees/CMakeLists.txt | 2 + cpp/algorithms/trees/binary/CMakeLists.txt | 2 +- cpp/cmake/CMakeLists.txt | 2 + cpp/cryptography/CMakeLists.txt | 2 + cpp/datastructs/CMakeLists.txt | 2 + cpp/graphics/CMakeLists.txt | 2 + cpp/graphics/README.md | 21 +++++- cpp/graphics/opengl-cmake/CMakeLists.txt | 2 - cpp/patterns/CMakeLists.txt | 2 + 23 files changed, 105 insertions(+), 32 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 76a63b2..ca273f9 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -17,6 +17,8 @@ project( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(algorithms) add_subdirectory(cmake) add_subdirectory(cryptography) diff --git a/cpp/README.md b/cpp/README.md index 80801a0..12c17f7 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -1,13 +1,13 @@ # Cpp -``` +```bash shaunrd0/klips/cpp/ -├── algorithms # Examples of various algorithms written in C++ -├── cmake # Example of using cmake to build and organize larger projects -├── cryptography# Examples of encrypting / decrypting using ciphers in C++ -├── datastructs # Collection of useful datastructures written in C++ -├── graphics # Examples of graphics projects written in C++ -├── patterns # Examples of various design patterns written in C++ +├── algorithms # Examples of various algorithms written in C++ +├── cmake # Example of using cmake to build and organize larger projects +├── cryptography # Examples of encrypting / decrypting using ciphers in C++ +├── datastructs # Collection of useful datastructures written in C++ +├── graphics # Examples of graphics projects written in C++ +├── patterns # Examples of various design patterns written in C++ └── README.md ``` @@ -15,15 +15,68 @@ This directory contains a `CMakeLists.txt`, which can be selected to open as a project within your preferred IDE. From there, all nested examples can be built, debugged, and ran. -In general, if a `CMakeLists.txt` is included in the project's root directory, -we can build the example with the following commands +Some of the more recent projects in this repository requires the latest CMake LTS. +To install `cmake` LTS with `apt` we can follow [official instructions from kitware](https://apt.kitware.com/) +Alternatively, we can install the LTS with python's `pip`. +```bash +sudo apt install python3.9 python3-pip +sudo apt purge cmake +python3.9 -m pip install -U pip +python3.9 -m pip install --upgrade cmake +# The command below is optional if you have added the python binary path to your PATH environment variable +sudo ln -s /usr/local/lib/python3.9/dist-packages/cmake/data/bin/cmake /usr/bin/ +cmake --version +cmake version 3.22.1 ``` + +Once cmake is installed, dependencies for all examples can be installed with the command below. +```bash +sudo apt install libsdl2-dev freeglut3-dev +``` + +If we build from this directory, we build all C++ projects and examples +```bash +cd /path/to/klips/cpp/ mkdir build && cd build cmake .. && cmake --build . ``` +We can see the binaries output in the `build/bin/` folder +```bash +~/Code/klips/cpp/build$ ls bin/ +abstract-factory-test graph-test-templated SingleListDriver +adapter-test graph-test-weighted singleton-test +bridge-test HeapDriver StackDriver +bubble-sort heap-sort state-test +bucket-sort insertion-sort TemplatedDoubleListDriver +CircleDoubleDriver merge-sort TemplatedQueueDriver +CircleSingleDriver observer-test TemplatedStackDriver +columnar-transposition-test opengl-test TemplatedVectorDriver +counting-sort prototype-test test-bst +DoubleListDriver QueueDriver test-bst-algo +execute-hello quick-sort test-redblack +factory-test radix-sort VectorDriver +graph-test-object sdl-test visitor-test +graph-test-simple select-sort +``` + +We can also build from subdirectories. +To only build projects related to design patterns we build from the `patterns/` subdirectory, for example +```bash +cd /path/to/klips/cpp/patterns +mkdir build && cd build +cmake .. && cmake --build . +``` + +And we can again see the binaries output in the `build/bin/` folder +```bash +~/Code/klips/cpp/patterns/build$ ls bin/ +abstract-factory-test bridge-test observer-test singleton-test visitor-test +adapter-test factory-test prototype-test state-test +``` + If cmake is not being used in a project, it can be built with `g++` manually using the commands outlined in `*/.vscode/tasks.json`, or by using VSCode to open the example -and running the build task. - +and running the build task. +Check the header comments in the main source file for the example for instructions. diff --git a/cpp/algorithms/CMakeLists.txt b/cpp/algorithms/CMakeLists.txt index a657747..62d9e77 100644 --- a/cpp/algorithms/CMakeLists.txt +++ b/cpp/algorithms/CMakeLists.txt @@ -15,6 +15,8 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(graphs) add_subdirectory(sorting) add_subdirectory(trees) diff --git a/cpp/algorithms/graphs/CMakeLists.txt b/cpp/algorithms/graphs/CMakeLists.txt index a01c305..78d872b 100644 --- a/cpp/algorithms/graphs/CMakeLists.txt +++ b/cpp/algorithms/graphs/CMakeLists.txt @@ -15,6 +15,8 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(object) add_subdirectory(simple) add_subdirectory(templated) diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index cd64a6d..59de505 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -15,6 +15,8 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(bubble) add_subdirectory(bucket) add_subdirectory(count) diff --git a/cpp/algorithms/sorting/bubble/CMakeLists.txt b/cpp/algorithms/sorting/bubble/CMakeLists.txt index 8827364..5a5f1d9 100644 --- a/cpp/algorithms/sorting/bubble/CMakeLists.txt +++ b/cpp/algorithms/sorting/bubble/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(BubbleSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(bubble-sort "bubble-sort.cpp") add_library(lib-bubble "lib-bubble.cpp") diff --git a/cpp/algorithms/sorting/bucket/CMakeLists.txt b/cpp/algorithms/sorting/bucket/CMakeLists.txt index 7e3a0cb..79543d1 100644 --- a/cpp/algorithms/sorting/bucket/CMakeLists.txt +++ b/cpp/algorithms/sorting/bucket/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(BucketSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(bucket-sort "bucket-sort.cpp") add_library(lib-bucket "lib-bucket.cpp") diff --git a/cpp/algorithms/sorting/count/CMakeLists.txt b/cpp/algorithms/sorting/count/CMakeLists.txt index 4d216be..38c0bdf 100644 --- a/cpp/algorithms/sorting/count/CMakeLists.txt +++ b/cpp/algorithms/sorting/count/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(CountingSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(counting-sort "counting-sort.cpp") add_library(lib-counting "lib-counting.cpp") diff --git a/cpp/algorithms/sorting/heap/CMakeLists.txt b/cpp/algorithms/sorting/heap/CMakeLists.txt index 38ef02a..20a60dc 100644 --- a/cpp/algorithms/sorting/heap/CMakeLists.txt +++ b/cpp/algorithms/sorting/heap/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(HeapSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(heap-sort "heap-sort.cpp") add_library(lib-heap "lib-heap.cpp") diff --git a/cpp/algorithms/sorting/insertion/CMakeLists.txt b/cpp/algorithms/sorting/insertion/CMakeLists.txt index 5cec75f..02e273d 100644 --- a/cpp/algorithms/sorting/insertion/CMakeLists.txt +++ b/cpp/algorithms/sorting/insertion/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(InsertionSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(insertion-sort "insertion-sort.cpp") add_library(lib-insertion "lib-insertion.cpp") diff --git a/cpp/algorithms/sorting/merge/CMakeLists.txt b/cpp/algorithms/sorting/merge/CMakeLists.txt index d717ef6..230ef08 100644 --- a/cpp/algorithms/sorting/merge/CMakeLists.txt +++ b/cpp/algorithms/sorting/merge/CMakeLists.txt @@ -9,7 +9,6 @@ cmake_minimum_required(VERSION 3.17) project(MergeSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_executable(merge-sort "merge-sort.cpp") add_library(lib-merge "lib-merge.cpp") diff --git a/cpp/algorithms/sorting/quick/CMakeLists.txt b/cpp/algorithms/sorting/quick/CMakeLists.txt index 1c98edc..8c51589 100644 --- a/cpp/algorithms/sorting/quick/CMakeLists.txt +++ b/cpp/algorithms/sorting/quick/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(QuickSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(quick-sort "quick-sort.cpp") add_library(lib-quick "lib-quick.cpp") diff --git a/cpp/algorithms/sorting/radix/CMakeLists.txt b/cpp/algorithms/sorting/radix/CMakeLists.txt index 5206fb8..5c181a2 100644 --- a/cpp/algorithms/sorting/radix/CMakeLists.txt +++ b/cpp/algorithms/sorting/radix/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(RadixSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(radix-sort "radix-sort.cpp") add_library(lib-radix-counting "lib-counting.cpp") diff --git a/cpp/algorithms/sorting/selection/CMakeLists.txt b/cpp/algorithms/sorting/selection/CMakeLists.txt index d5ff95c..140ba07 100644 --- a/cpp/algorithms/sorting/selection/CMakeLists.txt +++ b/cpp/algorithms/sorting/selection/CMakeLists.txt @@ -9,8 +9,6 @@ cmake_minimum_required(VERSION 3.16) project(SelectionSort LANGUAGES CXX) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - add_executable(select-sort "select-sort.cpp") add_library(lib-select "lib-select.cpp") diff --git a/cpp/algorithms/trees/CMakeLists.txt b/cpp/algorithms/trees/CMakeLists.txt index 896dc44..80c2f3d 100644 --- a/cpp/algorithms/trees/CMakeLists.txt +++ b/cpp/algorithms/trees/CMakeLists.txt @@ -15,5 +15,7 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(binary) add_subdirectory(redblack) diff --git a/cpp/algorithms/trees/binary/CMakeLists.txt b/cpp/algorithms/trees/binary/CMakeLists.txt index 664c83d..ba7d12f 100644 --- a/cpp/algorithms/trees/binary/CMakeLists.txt +++ b/cpp/algorithms/trees/binary/CMakeLists.txt @@ -18,4 +18,4 @@ project ( add_library(lib-bst-algo "bst.cpp") add_executable(test-bst-algo "driver.cpp") -target_link_libraries(test-bst-algo lib-bst) +target_link_libraries(test-bst-algo lib-bst-algo) diff --git a/cpp/cmake/CMakeLists.txt b/cpp/cmake/CMakeLists.txt index cb9168f..4030215 100644 --- a/cpp/cmake/CMakeLists.txt +++ b/cpp/cmake/CMakeLists.txt @@ -10,6 +10,8 @@ # Define the version of CMake cmake_minimum_required(VERSION 3.15) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + # Define the your project name project(cmake-template) diff --git a/cpp/cryptography/CMakeLists.txt b/cpp/cryptography/CMakeLists.txt index e1576ba..3a553ca 100644 --- a/cpp/cryptography/CMakeLists.txt +++ b/cpp/cryptography/CMakeLists.txt @@ -15,4 +15,6 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(columnar-transposition) diff --git a/cpp/datastructs/CMakeLists.txt b/cpp/datastructs/CMakeLists.txt index 29f3b97..25a99f6 100644 --- a/cpp/datastructs/CMakeLists.txt +++ b/cpp/datastructs/CMakeLists.txt @@ -15,6 +15,8 @@ project ( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(binarysearchtree) add_subdirectory(circledoublelist) add_subdirectory(circlesinglelist) diff --git a/cpp/graphics/CMakeLists.txt b/cpp/graphics/CMakeLists.txt index 4b85960..d1e9ab3 100644 --- a/cpp/graphics/CMakeLists.txt +++ b/cpp/graphics/CMakeLists.txt @@ -17,5 +17,7 @@ project( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(opengl-cmake) add_subdirectory(sdl-cmake) diff --git a/cpp/graphics/README.md b/cpp/graphics/README.md index d3d6dfb..5718cf1 100644 --- a/cpp/graphics/README.md +++ b/cpp/graphics/README.md @@ -1,6 +1,7 @@ # Graphics -Example graphics programming projects written in C++ +Example graphics programming projects written in C++. +For a more complete example of a graphics application, see [shaunrd0/qtk](https://gitlab.com/shaunrd0/qtk) ``` klips/cpp/graphics @@ -10,3 +11,21 @@ klips/cpp/graphics ├── sdl-cmake # Barebones sdl application written in C++ built with cmake └── sdl # Barebones sdl application written in C++ built with gcc ``` + +Install dependencies for these examples with the following command + +```bash +sudo apt install libsdl2-dev freeglut3-dev +``` + +Then we can build the examples that have `CMakeLists.txt` configured. +If the example does not use CMake, the commands to build and run are found within the header comments of the main source file. + +```bash +cd /path/to/klips/cpp/graphics/ +mkdir build && cd build +cmake .. && cmake --build . +ls bin/ + +opengl-test sdl-test +``` diff --git a/cpp/graphics/opengl-cmake/CMakeLists.txt b/cpp/graphics/opengl-cmake/CMakeLists.txt index 952908c..06b9956 100644 --- a/cpp/graphics/opengl-cmake/CMakeLists.txt +++ b/cpp/graphics/opengl-cmake/CMakeLists.txt @@ -24,7 +24,6 @@ if (OPENGL_FOUND) # Link opengl-test executable to OpenGL target_include_directories(lib-opengl-test PUBLIC ${OPENGL_INCLUDE_DIR}) target_link_libraries(lib-opengl-test PUBLIC ${OPENGL_LIBRARIES}) - else() message( "Error: CMake was unable to find the OpenGL package\n" @@ -38,7 +37,6 @@ if (GLUT_FOUND) # Link lib-opengl-test executable to GLUT target_include_directories(lib-opengl-test PUBLIC ${GLUT_INCLUDE_DIR}) target_link_libraries(lib-opengl-test PUBLIC ${GLUT_LIBRARIES}) - else() message( "Error: CMake was unable to find the GLUT package\n" diff --git a/cpp/patterns/CMakeLists.txt b/cpp/patterns/CMakeLists.txt index 3eb2aae..714492f 100644 --- a/cpp/patterns/CMakeLists.txt +++ b/cpp/patterns/CMakeLists.txt @@ -15,6 +15,8 @@ project( LANGUAGES CXX ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + add_subdirectory(singleton) add_subdirectory(adapter) add_subdirectory(bridge)