Skip to content

Commit 3c98042

Browse files
committed
Resolve link ordering and scope link libs per target
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 #84 and #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.
1 parent 04d28bd commit 3c98042

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ endif
2828

2929
ifdef CONFIG_LINUX
3030
LOADABLE_EXTENSION=so
31-
CFLAGS += -lm
31+
# Link-time libraries, kept OUT of CFLAGS so they land AFTER the object/archive
32+
# inputs on each link line. GNU ld with --as-needed (the Debian/Ubuntu default)
33+
# drops a library placed before the inputs that reference it, which otherwise
34+
# leaves sqrt/pow/ceil (libm) and pthread_* (libsqlite3, built threadsafe)
35+
# undefined. See asg017/sqlite-vec#84 and #93.
36+
#
37+
# The loadable extension is just sqlite-vec.c: it references libm (sqrt/pow/...)
38+
# but no raw pthread/dl symbols (SQLite is resolved by the host at load time),
39+
# so it links libm only. dl/pthread are needed solely by targets that embed the
40+
# threadsafe SQLite amalgamation (cli, static-into-exe, test-unit). Keeping
41+
# dl/pthread off the loadable also fixes Android/Bionic cross-builds, which run
42+
# on a Linux host (so CONFIG_LINUX) but have no standalone libpthread.
43+
LOADABLE_LIBS += -lm
44+
LINK_LIBS += -lm -ldl -lpthread
3245
endif
3346

3447
ifdef CONFIG_WINDOWS
@@ -99,7 +112,7 @@ $(TARGET_LOADABLE): sqlite-vec.c sqlite-vec.h $(prefix)
99112
-Ivendor/ \
100113
-O3 \
101114
$(CFLAGS) \
102-
$< -o $@
115+
$< $(LOADABLE_LIBS) -o $@
103116

104117
$(TARGET_STATIC): sqlite-vec.c sqlite-vec.h $(prefix) $(OBJS_DIR)
105118
$(CC) -Ivendor/ $(CFLAGS) -DSQLITE_CORE -DSQLITE_VEC_STATIC \
@@ -145,8 +158,8 @@ $(TARGET_CLI): sqlite-vec.h $(LIBS_DIR)/sqlite-vec.a $(LIBS_DIR)/shell.a $(LIBS_
145158
-DSQLITE_ENABLE_STMT_SCANSTATUS -DSQLITE_ENABLE_BYTECODE_VTAB -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
146159
-DSQLITE_EXTRA_INIT=core_init \
147160
$(CFLAGS) \
148-
-ldl -lm \
149-
examples/sqlite3-cli/core_init.c $(LIBS_DIR)/shell.a $(LIBS_DIR)/sqlite3.a $(LIBS_DIR)/sqlite-vec.a -o $@
161+
examples/sqlite3-cli/core_init.c $(LIBS_DIR)/shell.a $(LIBS_DIR)/sqlite3.a $(LIBS_DIR)/sqlite-vec.a \
162+
$(LINK_LIBS) -o $@
150163

151164

152165
sqlite-vec.h: sqlite-vec.h.tmpl VERSION
@@ -204,7 +217,7 @@ test-loadable-watch:
204217
watchexec --exts c,py,Makefile --clear -- make test-loadable
205218

206219
test-unit:
207-
$(CC) -DSQLITE_CORE -DSQLITE_VEC_TEST -DSQLITE_VEC_ENABLE_RESCORE -DSQLITE_VEC_ENABLE_DISKANN=1 tests/test-unit.c sqlite-vec.c vendor/sqlite3.c -I./ -Ivendor $(CFLAGS) -o $(prefix)/test-unit && $(prefix)/test-unit
220+
$(CC) -DSQLITE_CORE -DSQLITE_VEC_TEST -DSQLITE_VEC_ENABLE_RESCORE -DSQLITE_VEC_ENABLE_DISKANN=1 tests/test-unit.c sqlite-vec.c vendor/sqlite3.c -I./ -Ivendor $(CFLAGS) $(LINK_LIBS) -o $(prefix)/test-unit && $(prefix)/test-unit
208221

209222
# Standalone sqlite3 CLI with vec0 compiled in. Useful for benchmarking,
210223
# profiling (has debug symbols), and scripting without .load_extension.
@@ -219,7 +232,7 @@ cli: sqlite-vec.h $(prefix)
219232
-Ivendor/ -I./ \
220233
$(CFLAGS) \
221234
vendor/sqlite3.c vendor/shell.c sqlite-vec.c examples/sqlite3-cli/core_init.c \
222-
-ldl -lm -o $(prefix)/sqlite3
235+
$(LINK_LIBS) -o $(prefix)/sqlite3
223236

224237
fuzz-build:
225238
$(MAKE) -C tests/fuzz all

0 commit comments

Comments
 (0)