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.
This commit is contained in:
sneeker committed 2022-06-26 15:32:31 +00:00
1 parent f751ccbde9
commit 776f5315d4
4 files changed
+65

No files matched your search

+19
View File
@@ -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"
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef MATHX_H
#define MATHX_H
#include <stddef.h>
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
+18
View File
@@ -0,0 +1,18 @@
#include "mathx.h"
#include <stdio.h>
#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;
}
+16
View File
@@ -0,0 +1,16 @@
#include "mathx.h"
#include <math.h>
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;
}