Search code examples
postgresqlrocksdb

Linker error while building a postgres extension


I am trying to build a postgres extension against rocksdb. This is my makefile

MODULES = pgtam
EXTENSION = pgtam
DATA = pgtam--0.0.1.sql
PG_CPPFLAGS += -I/usr/local/include
SHLIB_LINK += -L/usr/local/lib -lrocksdb -lz -lbz2 -lsnappy -lzstd

PG_CONFIG = /usr/local/pgsql/bin/pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

I get this error on running make

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Werror=unguarded-availability-new -Wendif-labels -Wmissing-format-attribute -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -Wno-compound-token-split-by-macro -g -ggdb -Og -g3 -fno-omit-frame-pointer  -fvisibility=hidden -I/usr/local/include -I. -I./ -I/usr/local/pgsql/include/server -I/usr/local/pgsql/include/internal  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk    -c -o pgtam.o pgtam.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Werror=unguarded-availability-new -Wendif-labels -Wmissing-format-attribute -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -Wno-compound-token-split-by-macro -g -ggdb -Og -g3 -fno-omit-frame-pointer  -fvisibility=hidden pgtam.o -L/usr/local/pgsql/lib -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk   -Wl,-dead_strip_dylibs   -fvisibility=hidden -bundle -bundle_loader /usr/local/pgsql/bin/postgres -o pgtam.dylib
Undefined symbols for architecture arm64:
  "_rocksdb_get", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _get_table in pgtam.o
      _get_table in pgtam.o
  "_rocksdb_open", referenced from:
      _mem_tableam_handler in pgtam.o
  "_rocksdb_options_create", referenced from:
      _mem_tableam_handler in pgtam.o
  "_rocksdb_options_destroy", referenced from:
      _mem_tableam_handler in pgtam.o
  "_rocksdb_options_optimize_level_style_compaction", referenced from:
      _mem_tableam_handler in pgtam.o
  "_rocksdb_options_set_create_if_missing", referenced from:
      _mem_tableam_handler in pgtam.o
  "_rocksdb_put", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _set_ntables in pgtam.o
  "_rocksdb_readoptions_create", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _get_table in pgtam.o
      _get_table in pgtam.o
  "_rocksdb_readoptions_destroy", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _get_table in pgtam.o
      _get_table in pgtam.o
  "_rocksdb_writeoptions_create", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _set_ntables in pgtam.o
  "_rocksdb_writeoptions_destroy", referenced from:
      _memam_relation_set_new_filelocator in pgtam.o
      _set_ntables in pgtam.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [pgtam.dylib] Error 1

I expect linking to be successful. Things I checked:

  1. I do have the dylib present in the included path /usr/local/lib/librocksdb.dylib
  2. The dylib does have the symbols in the error message
╰─$ nm /usr/local/lib/librocksdb.dylib | rg _rocksdb_get
00000000000493a4 T _rocksdb_get
00000000000494f8 T _rocksdb_get_cf
000000000004992c T _rocksdb_get_cf_with_ts
0000000000056854 T _rocksdb_get_column_family_metadata
00000000000568cc T _rocksdb_get_column_family_metadata_cf
0000000000049088 T _rocksdb_get_full_history_ts_low
000000000004beb0 T _rocksdb_get_latest_sequence_number
0000000000056598 T _rocksdb_get_options_from_string
000000000005b800 T _rocksdb_get_pinned
000000000005b94c T _rocksdb_get_pinned_cf
000000000004bbd0 T _rocksdb_get_updates_since
0000000000049738 T _rocksdb_get_with_ts

Solution

  • Answering my own question, this worked

    MODULE_big = pgtam
    EXTENSION = pgtam
    DATA = pgtam--0.0.1.sql
    
    PG_CPPFLAGS += -L/usr/local/lib
    SHLIB_LINK += -L/usr/local/lib -lrocksdb
    
    PG_CONFIG = /usr/local/pgsql/bin/pg_config
    PGXS := $(shell $(PG_CONFIG) --pgxs)
    include $(PGXS)
    

    Ref https://www.postgresql.org/message-id/flat/[email protected]#c710c8c908ebfc9dc9bb4efb37cb80dc