From 120ef25b84ab19f97de092673b0e6ba11766bf2c Mon Sep 17 00:00:00 2001 From: clzhao20 Date: Mon, 8 Apr 2024 15:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20init(cmake):=20init=20modern=20c?= =?UTF-8?q?make=20template?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clangd/index/app.cpp.585A27E459A42788.idx | Bin 0 -> 648 bytes .../clangd/index/lib.cpp.3E5A22867B4F266E.idx | Bin 0 -> 1272 bytes .../clangd/index/lib.hpp.C6091EB14F97B140.idx | Bin 0 -> 498 bytes .gitignore | 1 + CMakeLists.txt | 78 ++++++++++++++++++ Makefile | 34 ++++++++ README.md | 46 +++++++++++ apps/CMakeLists.txt | 4 + apps/app.cpp | 17 ++++ cmake/FindSomeLib.cmake | 1 + docs/CMakeLists.txt | 5 ++ docs/mainpage.md | 11 +++ externals/.git-keep | 0 include/modern/lib.hpp | 12 +++ src/CMakeLists.txt | 24 ++++++ src/lib.cpp | 21 +++++ tests/CMakeLists.txt | 20 +++++ tests/testlib.cpp | 11 +++ 18 files changed, 285 insertions(+) create mode 100644 .cache/clangd/index/app.cpp.585A27E459A42788.idx create mode 100644 .cache/clangd/index/lib.cpp.3E5A22867B4F266E.idx create mode 100644 .cache/clangd/index/lib.hpp.C6091EB14F97B140.idx create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 README.md create mode 100644 apps/CMakeLists.txt create mode 100644 apps/app.cpp create mode 100644 cmake/FindSomeLib.cmake create mode 100644 docs/CMakeLists.txt create mode 100644 docs/mainpage.md create mode 100644 externals/.git-keep create mode 100644 include/modern/lib.hpp create mode 100644 src/CMakeLists.txt create mode 100644 src/lib.cpp create mode 100644 tests/CMakeLists.txt create mode 100644 tests/testlib.cpp diff --git a/.cache/clangd/index/app.cpp.585A27E459A42788.idx b/.cache/clangd/index/app.cpp.585A27E459A42788.idx new file mode 100644 index 0000000000000000000000000000000000000000..cc2d5281a19468507e705adc265e4bf1ddea0705 GIT binary patch literal 648 zcmWIYbaQK9VqkDi@vO*AElFfyU|G6ty_%Y5JC#?0>2*^<~Yuu`)>LXy4l7m5(_X7!z*v9eJ>LVeEAi zrnn33P0Qab(lr8-H?}Wm#Lgv~%31t-%Y8Mdx%E@tJ@B6n5|7 z&E5aEu3|~|w-uYvee>nAmA6e>0#=@i6OSwoV?1+SyWm96T3(B<7thpGt+hOV?V@D- z)3{R;0%cbHXDs~6z5c|{{cF|5<&>&JQ}(Axa_9d#BD3t;q`&jK{m*Y|c`^HKvh0qV zi+Xn&Xx8hl<2iY8>w=A9!jToJ%|96HQ>UC|D6Y&+k_U#ef%p}P$M1?Fc^E_(82C6? zI5@xrPzC~uQqziyfy@v;A@vs%ZCDw3_&7N^`C-~%G$R*^wxXTiCNh7~?EtFgmg3fi zsY-84`|i|y{Qyvymy?$dCR|uycV6byrN4|Ue4GlLiZEe@qSTy3kSmIdl8fCz3_%b9 z0sJrq1A_pZ$;h{8?YpzDZflyb3h)aG3xU%SR8|ujGVqs?EWank& HVB`V-lga7> literal 0 HcmV?d00001 diff --git a/.cache/clangd/index/lib.cpp.3E5A22867B4F266E.idx b/.cache/clangd/index/lib.cpp.3E5A22867B4F266E.idx new file mode 100644 index 0000000000000000000000000000000000000000..926ed715d66d21e4f0c6723953156f43101d5509 GIT binary patch literal 1272 zcmWIYbaVT_!oc91;#rZKT9OE4G6(^2aY<38BohO}OjZVliaA?DVzX}>h+HkMZ%w$H zaq`LT6#nK7Q%t0rovzs|sg{Pk%2h?$g_C z@!xcw*7cSX&Q~tK6bmi+x43NOhKi&2bC&u_)WvV=UhFsBha>9NY~89r>)y>pCP|Mz zCvAe?K}XAoV*VH8nU(OJ>m)v19U^ z%5PRn_T5XXLcSWu(V0@c8P!}0T`6nqJfHC;?ecU}3Dq|y&Jq3<_v?6% z+J$+YXExT&_;#{<^Ov`No5SDmAF^Osz))P7o8$mYDYYN`bQ|p7iZe3_0(l@H!p+6a z17R{S6z%*rk@<^m2M>b)P=;5US4Wsrnp1~?lZlaER)|rUwJ0^M_%? zupp{pK1!Z8uo4i<|KlgRa}%@oDE_~f(Qr@g)tZyMQ-mY*&$iS z@RUPXN?KA>LQI@N3@*zcj9^M3m=Xx4ID#pSU?%6LNFSx;r z`(4|&9UhUj4Ej|fUS&qFk3H*8xuo@Iw{-dRS8LT38_#@5-6!9+cG~>eYi35d45d~2f>~{6}@)WQ(0Oz z{9nr&QF(ytKIdGgP|@%Ct?QMfdoJ+#glVh^mzZm?YjwN8zsZKK5}6P9ef>VJVOh7G z|7pkBupY@6rn5JDUmMiT*#5BWT=nsNj4a|e6d8&ubCVQ-p;ol>+eGFsx*a?Wz;I;e zmFCr9 + +#include + +#include +#include +#include + +int main() { + std::vector input = {1.2, 2.3, 3.4, 4.5}; + + auto [mean, moment] = accumulate_vector(input); + + fmt::print("Mean: {}, Moment: {}\n", mean, moment); + + return 0; +} diff --git a/cmake/FindSomeLib.cmake b/cmake/FindSomeLib.cmake new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/cmake/FindSomeLib.cmake @@ -0,0 +1 @@ + diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt new file mode 100644 index 0000000..f833062 --- /dev/null +++ b/docs/CMakeLists.txt @@ -0,0 +1,5 @@ +set(DOXYGEN_EXTRACT_ALL YES) +set(DOXYGEN_BUILTIN_STL_SUPPORT YES) + +doxygen_add_docs(docs modern/lib.hpp "${CMAKE_CURRENT_SOURCE_DIR}/mainpage.md" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/include") diff --git a/docs/mainpage.md b/docs/mainpage.md new file mode 100644 index 0000000..6f29cd9 --- /dev/null +++ b/docs/mainpage.md @@ -0,0 +1,11 @@ +# Documentation for Modern Library {#mainpage} + +This is the documentation for my simple example library. + +It is good documentation because: + +1. It exists. +2. I wrote it. +3. Everything is documented (pretty easy since there's only one function) + +The single provided function is `::accumulate_vector`. diff --git a/externals/.git-keep b/externals/.git-keep new file mode 100644 index 0000000..e69de29 diff --git a/include/modern/lib.hpp b/include/modern/lib.hpp new file mode 100644 index 0000000..a058e69 --- /dev/null +++ b/include/modern/lib.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +/// \brief Accumulate a vector to produce the mean and the first moment of the distribution. +/// +/// This computes the mean and the first moment of a vector of double values. +/// +std::tuple accumulate_vector( + const std::vector& values ///< The vector of values +); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e3c614d --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,24 @@ +# Note that headers are optional, and do not affect add_library, but they will not +# show up in IDEs unless they are listed in add_library. + +# Optionally glob, but only for CMake 3.12 or later: +# file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${ModernCMakeExample_SOURCE_DIR}/include/modern/*.hpp") +set(HEADER_LIST "${ModernCMakeExample_SOURCE_DIR}/include/modern/lib.hpp") + +# Make an automatic library - will be static or dynamic based on user setting +add_library(modern_library lib.cpp ${HEADER_LIST}) + +# We need this directory, and users of our library will need it too +target_include_directories(modern_library PUBLIC ../include) + +# This depends on (header only) boost +target_link_libraries(modern_library PRIVATE Boost::boost) + +# All users of this library will need at least C++11 +target_compile_features(modern_library PUBLIC cxx_std_11) + +# IDEs should put the headers in a nice place +source_group( + TREE "${PROJECT_SOURCE_DIR}/include" + PREFIX "Header Files" + FILES ${HEADER_LIST}) diff --git a/src/lib.cpp b/src/lib.cpp new file mode 100644 index 0000000..dc20885 --- /dev/null +++ b/src/lib.cpp @@ -0,0 +1,21 @@ +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace ba = boost::accumulators; + +std::tuple accumulate_vector(const std::vector& values) { + + ba::accumulator_set>> acc; + + std::for_each(std::begin(values), std::end(values), std::ref(acc)); + + return {ba::mean(acc), ba::moment<2>(acc)}; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..917e23d --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,20 @@ +# Testing library +FetchContent_Declare( + catch + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v2.13.6) +FetchContent_MakeAvailable(catch) +# Adds Catch2::Catch2 + +# Tests need to be added as executables first +add_executable(testlib testlib.cpp) + +# I'm using C++17 in the test +target_compile_features(testlib PRIVATE cxx_std_17) + +# Should be linked to the main library, as well as the Catch2 testing library +target_link_libraries(testlib PRIVATE modern_library Catch2::Catch2) + +# If you register a test, then ctest and make test will run it. +# You can also run examples and check the output, as well. +add_test(NAME testlibtest COMMAND testlib) # Command can be a target diff --git a/tests/testlib.cpp b/tests/testlib.cpp new file mode 100644 index 0000000..e4becee --- /dev/null +++ b/tests/testlib.cpp @@ -0,0 +1,11 @@ +#define CATCH_CONFIG_MAIN +#include +#include + +TEST_CASE( "Quick check", "[main]" ) { + std::vector values {1, 2., 3.}; + auto [mean, moment] = accumulate_vector(values); + + REQUIRE( mean == 2.0 ); + REQUIRE( moment == Approx(4.666666) ); +}