mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-20 16:29:31 +00:00
Complements the CBOR C++ bindings (../cpp_bindings) with the native path: a C++
program that links the library and calls the native `<name>` ABI directly,
passing {.ffi.} structs by value and reading typed struct returns
(EchoResponse) from the callback — no CBOR, no tinycbor. An RAII TimerNode
wrapper bridges the async FFI-thread callback to a synchronous API via
std::future. README spells out native (same-process) vs CBOR (IPC). Verified
end-to-end with `make run`.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
43 lines
1.1 KiB
Makefile
43 lines
1.1 KiB
Makefile
# Build the native (same-process) C++ example for the timer library.
|
|
#
|
|
# make run # build the Nim dylib + the C++ driver, then run it
|
|
# make clean
|
|
#
|
|
# Links the library directly and uses the native ABI (../c_bindings/my_timer.h)
|
|
# — no CBOR, no tinycbor. The Nim library is compiled from the repo root so its
|
|
# vendored Nimble dependencies resolve.
|
|
|
|
REPO_ROOT := $(abspath ../../..)
|
|
NIM_SRC := $(REPO_ROOT)/examples/timer/timer.nim
|
|
HDR_DIR := ../c_bindings
|
|
|
|
UNAME_S := $(shell uname -s)
|
|
ifeq ($(UNAME_S),Darwin)
|
|
LIBNAME := libmy_timer.dylib
|
|
RPATH := -Wl,-rpath,.
|
|
else
|
|
LIBNAME := libmy_timer.so
|
|
RPATH := -Wl,-rpath,'$$ORIGIN'
|
|
endif
|
|
|
|
CXX ?= c++
|
|
CXXFLAGS ?= -std=c++17 -Wall -Wextra -O2 -I$(HDR_DIR)
|
|
NIMFLAGS := --mm:orc -d:chronicles_log_level=WARN --app:lib --noMain \
|
|
--nimMainPrefix:libmy_timer
|
|
|
|
.PHONY: all run clean
|
|
|
|
all: example
|
|
|
|
$(LIBNAME):
|
|
cd $(REPO_ROOT) && nim c $(NIMFLAGS) -o:$(CURDIR)/$(LIBNAME) $(NIM_SRC)
|
|
|
|
example: main.cpp $(HDR_DIR)/my_timer.h $(LIBNAME)
|
|
$(CXX) $(CXXFLAGS) main.cpp -L. -lmy_timer $(RPATH) -o example
|
|
|
|
run: example
|
|
./example
|
|
|
|
clean:
|
|
rm -f example $(LIBNAME)
|