From 776f5315d474220793b2d2cb68871e0396d67270 Mon Sep 17 00:00:00 2001 From: sneeker Date: Sun, 26 Jun 2022 15:32:31 +0000 Subject: [PATCH] feat(examples): add a hello project with a library and a consumer Smallest project that exercises the whole pipeline: one static library and one executable that links against it. --- examples/hello/build.meow | 19 +++++++++++++++++++ examples/hello/include/mathx.h | 12 ++++++++++++ examples/hello/src/main.c | 18 ++++++++++++++++++ examples/hello/src/mathx/vec.c | 16 ++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 examples/hello/build.meow create mode 100644 examples/hello/include/mathx.h create mode 100644 examples/hello/src/main.c create mode 100644 examples/hello/src/mathx/vec.c diff --git a/examples/hello/build.meow b/examples/hello/build.meow new file mode 100644 index 0000000..f221367 --- /dev/null +++ b/examples/hello/build.meow @@ -0,0 +1,19 @@ +project hello + +toolchain { + cc clang + cflags -O2 -Wall -Wextra -std=c17 +} + +lib mathx { + srcs src/mathx/*.c + include include + ldflags -lm +} + +bin hello { + srcs src/main.c + include include + use mathx + define GREETING="meow" +} diff --git a/examples/hello/include/mathx.h b/examples/hello/include/mathx.h new file mode 100644 index 0000000..b8c7764 --- /dev/null +++ b/examples/hello/include/mathx.h @@ -0,0 +1,12 @@ +#ifndef MATHX_H +#define MATHX_H + +#include + +double vec_dot(const double *a, const double *b, size_t n); +double vec_norm(const double *a, size_t n); +double vec_sum(const double *a, size_t n); +double stats_mean(const double *a, size_t n); +double stats_stddev(const double *a, size_t n); + +#endif diff --git a/examples/hello/src/main.c b/examples/hello/src/main.c new file mode 100644 index 0000000..d214fd5 --- /dev/null +++ b/examples/hello/src/main.c @@ -0,0 +1,18 @@ +#include "mathx.h" +#include + +#ifndef GREETING +#define GREETING "hello" +#endif + +int main(void) { + double xs[] = {1.0, 2.0, 3.0, 4.0, 5.0}; + size_t n = sizeof xs / sizeof xs[0]; + + printf("%s\n", GREETING); + printf(" dot %.3f\n", vec_dot(xs, xs, n)); + printf(" norm %.3f\n", vec_norm(xs, n)); + printf(" mean %.3f\n", stats_mean(xs, n)); + printf(" stddev %.3f\n", stats_stddev(xs, n)); + return 0; +} diff --git a/examples/hello/src/mathx/vec.c b/examples/hello/src/mathx/vec.c new file mode 100644 index 0000000..37eaf46 --- /dev/null +++ b/examples/hello/src/mathx/vec.c @@ -0,0 +1,16 @@ +#include "mathx.h" +#include + +double vec_dot(const double *a, const double *b, size_t n) { + double sum = 0.0; + for (size_t i = 0; i < n; i++) sum += a[i] * b[i]; + return sum; +} + +double vec_norm(const double *a, size_t n) { return sqrt(vec_dot(a, a, n)); } + +double vec_sum(const double *a, size_t n) { + double s = 0.0; + for (size_t i = 0; i < n; i++) s += a[i]; + return s; +}