宝石安装错误:您首先需要安装开发工具(Windows)

166

此问题针对Windows操作系统

我尝试安装宝石 'sinatra-websocket',但当我运行 gem install sinatra-websocket 时,出现了以下错误...

ERROR: Failed to build gem native extension

... 以及其他内容...

C:/Ruby193/bin/ruby.exe extconf.rb
checking for main() in -lc... *** 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=C:/Ruby193/bin/ruby
    --with-thin_parser-dir
    --without-thin_parser-dir
    --with-thin_parser-include
    --without-thin_parser-include=${thin_parser-dir}/include
    --with-thin_parser-lib
    --without-thin_parser-lib=${thin_parser-dir}/lib
    --with-clib
    --without-clib
C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:619:in `try_func'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:845:in `block in have_library'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:840:in `have_library'
    from extconf.rb:4:in `<main>'

以下是 C:\Ruby193\lib\ruby\gems\1.9.1\gems\thin-1.6.3\ext\thin_parser 文件夹中的 mkmf 文件:

"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -DFD_SETSIZE=2048  -DFD_SETSIZE=2048  -O3 -fno-omit-frame-pointer -g -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration conftest.c  -L. -LC:/Ruby193/lib -L.   -march=i486   -lmsvcrt-ruby191  -lshell32 -lws2_32 -limagehlp -lshlwapi  "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <winsock2.h>
4: #include <windows.h>
5: int main(int argc, char **argv)
6: {
7:   return 0;
8: }
/* end */

我正在使用Windows 7。

为什么会出现这个错误?有一行写着“无法创建Makefile...”,但那是否相关呢?


mkmf.log文件可以在gem的安装目录下找到。 - emi
你是否安装了 C 编译器? - ian
1
我过去使用的是Mingw。从这里你可以尝试make -vgcc -v来检查。你可以尝试安装Ruby开发工具包 - ian
2
看看这个答案,它非常详细,比我能给出的任何建议都要好。https://dev59.com/qGbWa4cB1Zd3GeqPTRom#11484346 - ian
1
如果你在Mac上运行bundle install时遇到了这个问题——例如,对于puma 5.6.2,我已经发布了一个单独的SO帖子,因为这个问题是特定于Windows的(在Windows上没有XCode,所以所有这些XCode答案都非常令人困惑)https://dev59.com/C3ENtIcB2Jgan1zn72j6 - Jason FB
显示剩余2条评论
20个回答

5

以下是我针对mac OSX 的解决方案:

在终端应用程序中:

步骤1:通过以下脚本,确保您已安装Xcode开发工具:

xcode-select --install

步骤2:使用以下脚本升级您的Ruby:

brew install ruby

步骤3:再次尝试。


1
我尝试了地球上的所有方法,超过两天时间,在MacOS 10.15.7 catalina,xcode 12.3上尝试开发nativescript应用程序,最终这个解决方案对我有效,cocoapods成功安装。谢谢! - mesut

3

实际上,真正的问题出现在将macOS的主要版本升级到另一个版本并安装更新的Xcode时。

cd /Library/Developer/CommandLineTools
ls -l SDKs/
total 0
lrwxr-xr-x  1 root  wheel   14 Feb  4 21:41 MacOSX.sdk -> MacOSX11.1.sdk
drwxr-xr-x  8 root  wheel  256 Feb  4 21:43 MacOSX10.15.sdk
drwxr-xr-x  7 root  wheel  224 Feb  4 21:43 MacOSX11.1.sdk

在前往苹果开发者网站并手动下载CLI工具后,该文件夹包含以下内容:

$> ls -l SDKs/
lrwxr-xr-x  1 root  wheel   14 May  4 18:10 MacOSX.sdk -> MacOSX11.3.sdk
drwxr-xr-x  8 root  wheel  256 May  4 18:10 MacOSX10.15.sdk
drwxr-xr-x  7 root  wheel  224 Feb  4 21:43 MacOSX11.1.sdk
drwxr-xr-x  7 root  wheel  224 Mar 16 10:03 MacOSX11.3.sdk
lrwxr-xr-x  1 root  wheel   14 May  4 18:09 MacOSX11.sdk -> MacOSX11.3.sdk

您可能仍然需要使用以下方法重置您的Xcode:
$> sudo xcode-select --reset
$> sudo xcodebuild -license

使用以下方法进行验证:

$> sudo xcode-select --print-path
/Applications/Xcode.app/Contents/Developer

最后,如果您仍然看到编译某些内容时出现错误,则需要密切关注编译器错误并解决它。例如,在我的情况下,问题很简单,就是在 macOS Big Sur 中 clang 需要特殊的参数来添加声明(struct)。可以通过以下方式进行修复:
export CFLAGS="-fdeclspec $CFLAGS"

如果您感兴趣(或正在寻找Ruby 3.0.x在线答案的任何人),以下是错误内容:

/usr/local/Cellar/ruby/3.0.1/include/ruby-3.0.0/ruby/internal/fl_type.h:285:1: error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes

3
这意味着您没有编译工具安装。对于Mac系统,xcode-select --install即可安装编译工具。而对于Linux或其他系统,您可以使用包管理器,例如apt install cmake可以为使用apt包管理器的系统安装必要的构建工具。

2

我最近升级到了MAC OS大狮山,运行gem install时出现错误...

解决方法是完全删除CLT并重新运行安装。

可选:

brew upgrade

运行:

sudo rm -rf /Library/Developer/CommandLineTools

sudo xcode-select --install


2

针对 Ubuntu 20.04.3 LTS (Focal Fossa) -- WSL

在开始之前,需要解决一些依赖关系,可以尝试使用以下命令序列:

# if ERROR: 'Gem installation error: You have to install development tools first'
# BEFORE installing the dev-tools, install dependencies:
# if ERROR: 'GCC fatal error: stdio.h: No such file or directory'
$ sudo apt-get install libc6
# https://dev59.com/j2Ik5IYBdhLWcg3wG6zb#20150282
$ sudo apt-get install libc6-dev
$ sudo apt-get install libffi-dev

# Now, go for the development tools
# https://dev59.com/t14c5IYBdhLWcg3wj7F6#63442072
$ sudo apt-get install ubuntu-dev-tools

# Finally, try again
$ bundle update --bundler
# ... enjoy :)

参考资料:


2
这适用于我的Mac。
brew install cocoapods

0
在 Mac 上,我是通过执行以下操作来解决的:
sudo xcode-select --reset

我之前安装了xcode-select,但它无论如何都无法工作。通过使用--reset命令修复了这个问题。


0
在 macOS Catalina 上,运行 $ brew install coreutils (来源) 后,我按照 Evan Hoffman 的旧 文章 安装了 chef(其中嵌入了最新的稳定版 Ruby)。
然后,我可以安装任何需要的 gem,因为现在已经安装了所有必要的开发工具。 $ curl -L https://www.getchef.com/chef/install.sh | sudo bash $ sudo gem install -V <gem> 在 Big Sur 上可能会有所不同。

0
如果您安装了多个版本的Xcode,请确保从最新的版本中选择命令行工具,否则gem将无法找到它们。

-2
尝试rvm install ruby --latest
如果已经安装,请尝试rvm reinstall ruby --latest
然后使用$ sudo gem install -V 对我有效。

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