Skip to content

Fix link ordering so libm/pthread resolve in make all#304

Open
stumpylog wants to merge 1 commit into
asg017:mainfrom
stumpylog:fix/lib-linking
Open

Fix link ordering so libm/pthread resolve in make all#304
stumpylog wants to merge 1 commit into
asg017:mainfrom
stumpylog:fix/lib-linking

Conversation

@stumpylog

Copy link
Copy Markdown

Problem

On Linux, make all fails at link time:

undefined reference to `sqrt' / `pow' / `ceil'

-lm lives in CFLAGS, which is expanded before the archive inputs on each link line. GNU ld --as-needed (Debian/Ubuntu default) then drops libm before it reaches sqlite-vec.a. Same trap for libpthread (sqlite is built threadsafe but -lpthread is never linked).

Fixes #84, #93, #2.

Fix

Move link libraries out of CFLAGS into a dedicated LINK_LIBS appended after the inputs on every link line.

-CFLAGS += -lm
+LINK_LIBS += -lm -ldl -lpthread

Verification

make clean && make all                              # was failing, now exits 0
dist/sqlite3 :memory: "SELECT vec_version()"        # v0.1.10-alpha.4

Complementary to #290/#292, which add $(CFLAGS) to compile lines but don't touch the -lm link ordering.

@stumpylog stumpylog marked this pull request as ready for review June 11, 2026 13:59
Keep link-time libraries out of CFLAGS so they land AFTER the object/archive
inputs on each link line. GNU ld with --as-needed (the Debian/Ubuntu default)
drops a library placed before the inputs that reference it, leaving libm
(sqrt/pow/ceil) and pthread_* (from the threadsafe libsqlite3) undefined.
See asg017#84 and asg017#93.

Scope the libraries per target: the loadable extension is just sqlite-vec.c,
which references libm but no raw pthread/dl symbols (SQLite is resolved by the
host at load time), so it links libm only via LOADABLE_LIBS. dl and pthread
stay in LINK_LIBS for the targets that embed the threadsafe SQLite amalgamation
(cli, static-into-exe, test-unit). This also fixes Android/Bionic cross-builds,
which run on a Linux host (CONFIG_LINUX) but have no standalone libpthread.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

undefined reference to `sqrt'

1 participant