编译PostgreSQL扩展时出现RocksDB链接错误
基于RocksDB构建PostgreSQL扩展的链接错误解决
问题描述
我正在尝试基于RocksDB构建PostgreSQL扩展,使用的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)
执行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
我已经确认以下两点:
/usr/local/lib路径下存在librocksdb.dylib文件- 该库包含错误提示中缺失的符号,执行
nm /usr/local/lib/librocksdb.dylib | rg _rocksdb_get的输出如下:
╰─$ 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
解决方法
核心问题是链接库的顺序错误,在链接过程中,被依赖的库需要放在依赖它的库之后。RocksDB依赖各类压缩库,因此需要调整SHLIB_LINK中的参数顺序。
1. 调整Makefile的链接顺序
修改SHLIB_LINK行,将-lrocksdb移到所有压缩库之后:
SHLIB_LINK += -L/usr/local/lib -lz -lbz2 -lsnappy -lzstd -lrocksdb
2. 额外验证项
- 确认RocksDB库是针对arm64架构编译的:
执行file /usr/local/lib/librocksdb.dylib,输出应包含arm64标识:/usr/local/lib/librocksdb.dylib: Mach-O 64-bit dynamically linked shared library arm64 - 确认PostgreSQL也是arm64架构版本:
执行file /usr/local/pgsql/bin/postgres验证输出中的架构信息。
内容的提问来源于stack exchange,提问作者ACC
相关产品推荐
相关产品推荐

