在 macOS Sierra 上安装 Ruby Sqlite3,如何使用 sqlite3_libversion_number() 函数?

5
我正在尝试安装Metasploit框架(不重要),但是bundler在尝试安装sqlite3时一直失败。Sqlite3已经安装(在命令行中执行sqlite3可以进入环境),并使用brew link sqlite3进行链接(加上--force选项,原因不明),但每次bundler install都会失败,并出现以下错误:
sudo gem install sqlite3
Building native extensions.  This could take a while...
ERROR:  Error installing sqlite3:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
checking for sqlite3.h... yes
checking for pthread_create() in -lpthread... yes
checking for sqlite3_libversion_number() in -lsqlite3... no
sqlite3 is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --with-sqlite3-config
    --without-sqlite3-config
    --with-pkg-config
    --without-pkg-config
    --with-sqlite3-dir
    --without-sqlite3-dir
    --with-sqlite3-include
    --without-sqlite3-include=${sqlite3-dir}/include
    --with-sqlite3-lib
    --without-sqlite3-lib=${sqlite3-dir}/
    --with-pthreadlib
    --without-pthreadlib
    --with-sqlite3lib
    --without-sqlite3lib


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.12 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.12/ext/sqlite3/gem_make.out

我也遇到了同样的问题,尝试安装 gem install sequel mysql sqlite3 以便像 https://dev59.com/RG865IYBdhLWcg3wSMkw#11328913 中那样从mysql转换到sqlite,你找到解决方案了吗? - Zack Morris
3个回答

21

我最终通过在macOS Sierra 10.12.5(16F73)上指定内置的Mac OS X sqlite库目录来解决了这个问题:

$ whereis sqlite3
/usr/bin/sqlite3
# if binary is in /usr/bin then library is typically in /usr/lib
$ gem install sqlite3 -- --with-sqlite3-lib=/usr/lib
Building native extensions with: '--with-sqlite3-lib=/usr/lib'
This could take a while...
Successfully installed sqlite3-1.3.13
Parsing documentation for sqlite3-1.3.13
Done installing documentation for sqlite3 after 0 seconds
1 gem installed

我尝试指定Homebrew库目录,但出于某种原因它没有起作用:

$ brew ls --verbose sqlite3
/usr/local/Cellar/sqlite/3.19.3/.brew/sqlite.rb
/usr/local/Cellar/sqlite/3.19.3/bin/sqlite3
/usr/local/Cellar/sqlite/3.19.3/include/msvc.h
/usr/local/Cellar/sqlite/3.19.3/include/sqlite3.h
/usr/local/Cellar/sqlite/3.19.3/include/sqlite3ext.h
/usr/local/Cellar/sqlite/3.19.3/INSTALL_RECEIPT.json
/usr/local/Cellar/sqlite/3.19.3/lib/libsqlite3.0.dylib
/usr/local/Cellar/sqlite/3.19.3/lib/libsqlite3.a
/usr/local/Cellar/sqlite/3.19.3/lib/libsqlite3.dylib
/usr/local/Cellar/sqlite/3.19.3/lib/pkgconfig/sqlite3.pc
/usr/local/Cellar/sqlite/3.19.3/README.txt
/usr/local/Cellar/sqlite/3.19.3/share/man/man1/sqlite3.1
$ gem install sqlite3 -- --with-sqlite3-lib=/usr/local/Cellar/sqlite/3.19.3/lib
This could take a while...
ERROR:  Error installing sqlite3:
    ERROR: Failed to build gem native extension.
...

如果有人知道如何指定Homebrew库目录,请告诉我,因为这将提供更多的安装控制(据说MacPorts可以工作,但我不再使用它)。


对于任何好奇的人,这是安装Ruby的Sequel的完整命令:

gem install sequel mysql sqlite3 -- --with-sqlite3-lib=/usr/lib

如何将在主机端口3306上侦听的Laravel Homestead MySQL数据库转换为SQLite,参考我的评论

sequel mysql://homestead:secret@192.168.10.10:3306/my_database -C sqlite://my_database.sqlite

1
2017年11月20日的命令'gem install sqlite3 -- --with-sqlite3-lib=/usr/lib'完全解决了我的问题,谢谢! - NamNamNam
如果您正在使用Bundler,这应该使其在运行Bundler的所有位置上工作:bundle config set --global build.sqlite3 --with-sqlite3-lib=/usr/lib - Jim Meyer

5

在我的Mac High Sierra操作系统10.13上,这个方法对我有效。

gem install sqlite3 -- --with-sqlite3-lib=/usr/lib

有没有办法直接从Gemfile使这个工作起来?像这样:# 添加到Gemfile gem 'sqlite3',:lib =>“/usr/lib/”,:condition =>:only_if_mac - Riccardo
2
这是最终对我起作用的命令:bundle config build.sqlite3 --with-sqlite3-include=$HOME/include --with-sqlite3-lib=$HOME/lib --with-sqlite3-dir=$HOME/bin - Tatiana Frank
为了让bundler正常工作:bundle config build.sqlite3 --with-sqlite3-lib=/usr/lib - Jim Meyer

1
深入研究 mkmf.log,你可以通过替换指向安装尝试日志文件指针的最后一部分来找到它。在我的情况下:
Results logged to ~/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/sqlite3-1.3.13/gem_make.out

所以我在这里找到了 mkmf.log
~/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/sqlite3-1.3.13/mkmf.log

我找到了一个更明确的错误描述:
conftest.c:13:15: error: implicit declaration of function 'pthread_create' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

conftest.c:13:15: error: implicit declaration of function 'sqlite3_libversion_number' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

所以添加--with-cflags="-Wno-error=implicit-function-declaration"就解决了问题!
gem install sqlite3 -- --with-cflags="-Wno-error=implicit-function-declaration"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接