在Apple Silicon上用Clang构建SQLite扩展可行吗?需改用GCC吗?
问题描述
在M1(arm64架构)笔记本上尝试构建SQLite percentile扩展时,执行以下编译命令:
gcc -g -fPIC -shared percentile.c -o percentile.so
返回链接错误:
$ gcc -g -fPIC -shared percentile.c -o percentile.so Undefined symbols for architecture arm64: "_sqlite3_aggregate_context", referenced from: _percentStep in percentile-a11598.o _percentFinal in percentile-a11598.o "_sqlite3_create_function", referenced from: _sqlite3_percentile_init in percentile-a11598.o _sqlite3_percentile_init in percentile-a11598.o _sqlite3_percentile_init in percentile-a11598.o "_sqlite3_free", referenced from: _percentStep in percentile-a11598.o _percentFinal in percentile-a11598.o "_sqlite3_realloc64", referenced from: _percentStep in percentile-a11598.o "_sqlite3_result_double", referenced from: _percentFinal in percentile-a11598.o "_sqlite3_result_error", referenced from: _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o "_sqlite3_result_error_nomem", referenced from: _percentStep in percentile-a11598.o "_sqlite3_user_data", referenced from: _percentStep in percentile-a11598.o "_sqlite3_value_double", referenced from: _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o "_sqlite3_value_numeric_type", referenced from: _percentStep in percentile-a11598.o _percentStep in percentile-a11598.o "_sqlite3_value_type", referenced from: _percentStep in percentile-a11598.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
请问是否可以使用Clang构建SQLite扩展,还是必须使用GCC才能解决问题?
解决方案
首先明确:M1 macOS上的gcc其实是Clang的别名,你当前用的就是Clang编译,错误根源不是编译器类型,而是编译命令缺少SQLite库的链接参数。
SQLite扩展需要链接SQLite开发库才能找到那些未定义的符号,正确的编译命令需加上-lsqlite3参数:
clang -g -fPIC -shared percentile.c -o percentile.so -lsqlite3
或者直接在你之前的gcc命令后添加该参数也可行:
gcc -g -fPIC -shared percentile.c -o percentile.so -lsqlite3
总结:
- Clang完全可以用来构建SQLite扩展,不需要专门安装GCC
- 错误是因为未链接SQLite库,添加
-lsqlite3参数即可解决
内容的提问来源于stack exchange,提问作者merlin2011
相关产品推荐
相关产品推荐

