Skip to content

Commit 0f8ffda

Browse files
committed
Resolves link ordering issues
1 parent 04d28bd commit 0f8ffda

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ 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+
LINK_LIBS += -lm -ldl -lpthread
3237
endif
3338

3439
ifdef CONFIG_WINDOWS
@@ -99,7 +104,7 @@ $(TARGET_LOADABLE): sqlite-vec.c sqlite-vec.h $(prefix)
99104
-Ivendor/ \
100105
-O3 \
101106
$(CFLAGS) \
102-
$< -o $@
107+
$< $(LINK_LIBS) -o $@
103108

104109
$(TARGET_STATIC): sqlite-vec.c sqlite-vec.h $(prefix) $(OBJS_DIR)
105110
$(CC) -Ivendor/ $(CFLAGS) -DSQLITE_CORE -DSQLITE_VEC_STATIC \
@@ -145,8 +150,8 @@ $(TARGET_CLI): sqlite-vec.h $(LIBS_DIR)/sqlite-vec.a $(LIBS_DIR)/shell.a $(LIBS_
145150
-DSQLITE_ENABLE_STMT_SCANSTATUS -DSQLITE_ENABLE_BYTECODE_VTAB -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
146151
-DSQLITE_EXTRA_INIT=core_init \
147152
$(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 $@
153+
examples/sqlite3-cli/core_init.c $(LIBS_DIR)/shell.a $(LIBS_DIR)/sqlite3.a $(LIBS_DIR)/sqlite-vec.a \
154+
$(LINK_LIBS) -o $@
150155

151156

152157
sqlite-vec.h: sqlite-vec.h.tmpl VERSION
@@ -204,7 +209,7 @@ test-loadable-watch:
204209
watchexec --exts c,py,Makefile --clear -- make test-loadable
205210

206211
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
212+
$(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
208213

209214
# Standalone sqlite3 CLI with vec0 compiled in. Useful for benchmarking,
210215
# profiling (has debug symbols), and scripting without .load_extension.
@@ -219,7 +224,7 @@ cli: sqlite-vec.h $(prefix)
219224
-Ivendor/ -I./ \
220225
$(CFLAGS) \
221226
vendor/sqlite3.c vendor/shell.c sqlite-vec.c examples/sqlite3-cli/core_init.c \
222-
-ldl -lm -o $(prefix)/sqlite3
227+
$(LINK_LIBS) -o $(prefix)/sqlite3
223228

224229
fuzz-build:
225230
$(MAKE) -C tests/fuzz all

0 commit comments

Comments
 (0)