consumer — the smallest project that links doppler¶
Get the code¶
The files are listed at the bottom of this page. The tabs there show the project on the development branch; this fetches the project's folder from the latest release, which can differ from them until the next release:
curl -fsSL https://github.com/doppler-dsp/doppler/archive/refs/tags/v0.88.0.tar.gz \
| tar -xz --strip-components=2 doppler-0.88.0/example-projects/consumer
Or browse example-projects/consumer/.
One source file, one find_package(doppler), two link modes. If you are
adding doppler to an existing build, start here: everything else in
example-projects/ is this plus a subject.
It runs the local oscillator at a quarter of the sample rate, where the output is known exactly (1, j, -1, -j, repeating), and exits 0 only if the samples match. It uses the pure-C core alone, so it is the same program on Linux, macOS and Windows. The oscillator is not the point — the link line is.
What it demonstrates¶
find_package(doppler)against an install prefix, which is how a downstream consumes a release rather than a source tree.- Both link targets.
doppler::doppleris the shared library;doppler::doppler-staticis the self-contained static one. Both binaries print the same thing, which is the point of building the pair — a difference between them is a packaging bug, not a program one. - Three build faces, all equivalent. CMake is below;
ccandpkg-configare in the next section, and CI asserts all three produce identical output (make consumer-faces-check).
Build and run¶
If doppler came from the .deb or .rpm packages (or any system install), it
is already on the compiler's paths and this is all of it. You need cmake,
gcc, make and pkg-config; on Debian or Ubuntu that is
sudo apt install cmake gcc make pkg-config.
If doppler is in a prefix of your own (the release tarball, jbx get-doppler,
~/.local), say where:
From a source tree, install it into a prefix first, then the same:
make help lists the knobs; make clean removes build/.
The Makefile is a thin wrapper — the two commands it runs are:
Without CMake¶
The same program, compiled directly:
or letting pkg-config answer:
Copying it¶
Take CMakeLists.txt and the find_package block. main.c is a placeholder
for your program.
The files¶
cmake_minimum_required(VERSION 3.16)
project(doppler_consumer C)
# A downstream finds doppler with find_package. Point CMake at an install
# prefix (a `cmake --install` tree or an extracted release tarball) with
# -DCMAKE_PREFIX_PATH=/path/to/doppler-install
# or rely on a system install. The package provides two link targets:
#
# doppler::doppler shared — links `-ldoppler`; smallest binary.
# doppler::doppler-static static — pure C, self-contained: -lm, -lpthread.
# doppler::stream[-static] OPTIONAL stream layer (NATS transport);
# link only if you need dp_pub_*/dp_sub_* or nats://.
#
# This example builds one executable per mode so both are exercised; a real
# consumer just picks the target matching its linking policy.
# <!-- doc-version:start -->
find_package(doppler 0.88.0 REQUIRED)
# <!-- doc-version:end -->
add_executable(consumer_shared main.c)
target_link_libraries(consumer_shared PRIVATE doppler::doppler)
if(TARGET doppler::doppler-static)
add_executable(consumer_static main.c)
target_link_libraries(consumer_static PRIVATE doppler::doppler-static)
endif()
# example-projects/consumer — a downstream project's own Makefile.
#
# The smallest complete thing that links doppler: find_package, one source
# file, two link modes. This wraps the two CMake commands you would otherwise
# type; `make help` lists everything.
#
# CMake is only one of the three ways this project is known to build — see
# README.md for the `cc` and `pkg-config` lines, which CI also exercises.
BUILD_DIR ?= build
CMAKE_BUILD_TYPE ?= Release
# Where doppler is installed. Empty means "a system install CMake can already
# find"; set it to a `cmake --install` tree or an extracted release tarball:
# make PREFIX=~/.local run
PREFIX ?=
CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
ifneq ($(strip $(PREFIX)),)
CMAKE_FLAGS += -DCMAKE_PREFIX_PATH=$(abspath $(PREFIX))
endif
.PHONY: all build run clean help
.DEFAULT_GOAL := build
all: build
# Configure every time rather than gating on CMakeCache.txt: re-running
# configure is cheap and idempotent, while a cached one silently ignores a
# changed PREFIX — which is the mistake this target exists to prevent.
build:
cmake -S . -B $(BUILD_DIR) $(CMAKE_FLAGS)
cmake --build $(BUILD_DIR)
# Both link modes print the SAME thing. That is the point of the pair: the
# static and shared targets are two routes to one library, and a difference
# between them is a packaging bug rather than a program one.
run: build
$(BUILD_DIR)/consumer_shared
@if [ -x $(BUILD_DIR)/consumer_static ]; then \
echo; echo "── again, linked statically ──"; \
$(BUILD_DIR)/consumer_static; \
else \
echo "note: no static target — this install ships no doppler::doppler-static"; \
fi
# The program writes its output beside itself when run in place, so clean
# takes that too -- otherwise `make clean` leaves the interesting half behind.
clean:
rm -rf $(BUILD_DIR)
help:
@echo "consumer — the smallest project that links doppler"
@echo
@echo " make build configure and compile"
@echo " make run build, then run both link modes"
@echo " make clean remove $(BUILD_DIR)/"
@echo
@echo " PREFIX=<dir> where doppler is installed"
@echo " CMAKE_BUILD_TYPE=<t> default Release"
@echo " BUILD_DIR=<dir> default build"
/*
* main.c — a minimal downstream consumer of the doppler C library.
*
* Runs the local oscillator at a quarter of the sample rate, where its output
* is known exactly: the phasor turns 90 degrees per sample, cycling
* 1, j, -1, -j. Exit status 0 means the library linked, ran and produced
* those samples -- not merely that it linked.
*
* It then does the three things a header-only reader of the API would not
* expect to matter to a LINK, and do (doppler#1450). Most of doppler's C
* surface is inline -- every `step()`, the thread wrappers, the whole ring
* buffer -- so the calls those bodies make land in THIS object file, and the
* linker resolves them from what the consumer was handed, never from
* libdoppler's own dependencies:
*
* - an inline `dp_agc_step()`, whose body calls libm;
* - a `dp_thread.h` thread, which is pthread on POSIX;
* - a ring buffer, which maps its own pages.
*
* With only `-ldoppler` on the line the first fails everywhere and the second
* on any glibc older than 2.34. `lo` alone never showed it: its kernel is
* out of line, so nothing of libm's is referenced from here.
*
* It uses only the pure-C core, so it builds and runs on every platform
* doppler publishes for, Windows included, and links with exactly what
* find_package or pkg-config hand out -- nothing added by hand. It writes no
* file.
*
* Builds against either link mode; see CMakeLists.txt (find_package) and the
* pkg-config commands in docs/install/c.md.
*/
#include "doppler/dp_complex.h"
#include "doppler/dp_thread.h"
#include <doppler/agc/agc_core.h>
#include <doppler/buffer/buffer.h>
#include <doppler/lo/lo_core.h>
#include <math.h>
#include <stdio.h>
#define N 8
/* Runs on its own thread and reports back through the argument, so the join
* is observable rather than assumed. */
DP_THREAD_FN (set_flag, arg)
{
*(int *)arg = 1;
DP_THREAD_RETURN;
}
/* The inline surface. `n` comes from argc so the compiler cannot fold the
* loop away and with it the libm references the test exists to create. */
static int
inline_surface (int n)
{
int bad = 0;
/* An AGC driven by a constant converges on its target level: the inline
* step() ran, and its libm calls resolved. */
dp_agc_state_t *agc = dp_agc_create (0.0, 0.0025, 0.05);
if (!agc)
return 1;
float _Complex y = 0.0f;
for (int i = 0; i < 4000 * n; i++)
y = dp_agc_step (agc, 0.1f);
dp_agc_destroy (agc);
if (fabsf (cabsf (y) - 1.0f) > 0.05f)
{
fprintf (stderr, "agc settled at %f, want 1.0\n", (double)cabsf (y));
bad++;
}
int flag = 0;
dp_thread_t t;
if (dp_thread_create (&t, set_flag, &flag) != 0)
return bad + 1;
dp_thread_join (t);
if (!flag)
{
fprintf (stderr, "the joined thread never ran\n");
bad++;
}
dp_f32_t *ring = dp_f32_create (1000);
if (!ring)
{
fprintf (stderr, "dp_f32_create failed\n");
return bad + 1;
}
dp_f32_destroy (ring);
return bad;
}
int
main (int argc, char **argv)
{
(void)argv;
static const float want_re[4] = { 1.0f, 0.0f, -1.0f, 0.0f };
static const float want_im[4] = { 0.0f, 1.0f, 0.0f, -1.0f };
dp_lo_state_t *lo = dp_lo_create (0.25);
if (!lo)
{
fprintf (stderr, "dp_lo_create failed\n");
return 1;
}
float _Complex x[N];
dp_lo_steps (lo, N, x, N);
dp_lo_destroy (lo);
int bad = 0;
for (int i = 0; i < N; i++)
if (fabsf (crealf (x[i]) - want_re[i % 4]) > 1e-5f
|| fabsf (cimagf (x[i]) - want_im[i % 4]) > 1e-5f)
bad++;
printf ("lo at fs/4: %d of %d samples off the 1, j, -1, -j cycle\n", bad, N);
int inl = inline_surface (argc);
printf ("inline surface (agc step, thread, ring): %d failure(s)\n", inl);
return bad != 0 || inl != 0;
}