关于rails上的ruby:无法构建本机扩展

Failure to build native extension

我是rails的新手,并试图从SQLite3将我的默认DB切换到Postgres。 我遇到了一个错误。 我正在使用gem install pg并收到此错误错误:安装pg时出错:错误:无法构建gem本机扩展。 我正在使用Mavericks OSX和Ruby 1.9.3,Rails 3.2.12和PG 9.3--目标是将一个示例应用程序部署到heroku(它似乎不支持SQLite。

我试过的步骤:

链接1:在OS X上安装PG gem - 无法构建本机扩展

链接2(heroku说明):https://devcenter.heroku.com/articles/sqlite3

编辑包含以下特定错误消息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
HEAD https://rubygems.org/latest_specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/latest_specs.4.8.gz
304 NOT Modified
HEAD https://rubygems.org/specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/specs.4.8.gz
304 NOT Modified
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/.gemtest
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/BSDL
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ChangeLog
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Contributors.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/History.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/LICENSE
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Manifest.txt
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/POSTGRES
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README-OS_X.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README-Windows.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README.ja.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README.rdoc
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Rakefile
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Rakefile.cross
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.def
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.txt
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/extconf.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/gvl_wrappers.c
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/gvl_wrappers.h
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg.c
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg.h
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_connection.c
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_errors.c
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_result.c
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg.sln
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg_18/pg.vcproj
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg_19/pg_19.vcproj
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/connection.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/constants.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/exceptions.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/RESULT.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/array_insert.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_api.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_copyto.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_mixed.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/check_conn.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/copyfrom.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/copyto.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/cursor.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/disk_usage_report.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/issue-119.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/losample.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/minimal-testcase.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/notify_wait.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/pg_statistics.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/replication_monitor.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/test_binary_values.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/wal_shipper.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/warehouse_partitions.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/DATA/expected_trace.out
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/DATA/random_binary_data
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/lib/helpers.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg/connection_spec.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg/result_spec.rb
/usr/LOCAL/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg_spec.rb
Building native extensions.  This could take a while...
/usr/LOCAL/rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking FOR pg_config... yes
USING config VALUES FROM /usr/LOCAL/bin/pg_config
*** 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=/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby
    --with-pg
    --without-pg
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
/usr/LOCAL/rvm/rubies/ruby-1.9.3-p392/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 /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:461:in `
try_link0'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'

    FROM extconf.rb:39:IN `<main>'
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    Building has failed. See above output for more information on the failure.

Gem files will remain installed in /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/pg-0.17.0 for inspection.
Results logged to /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/pg-0.17.0/ext/gem_make.out


感谢你的帮助。我在终端中运行以下命令解决了具体问题

1
brew install apple-gcc42

为了澄清,我有最新的XCode(带命令行)和Mavericks,Ruby 1.9.3,Rails 3.2.12和PG 9.3。我不确定为什么当我已安装命令行工具时,该特定命令有效。输出的一个变化

1
gcc--version

那是--prefix=/Library/Developer/CommandLineTools/usr

改变为

1
--prefix=/Applications/Xcode.app/Contents/Developer/usr

同样,这种变化在实践中起作用,但我不确定为什么这个固定的东西。


它正在尝试构建gem pg,其中包含C或C ++代码,并且编译阶段不起作用。在OS X下,您必须通过其他方式安装XCode或获取编译器,并且您用于运行bundle install的shell必须在其路径中包含编译器。虽然我还没有升级到Mavericks,但我已经在OS X下多次编译pg了。

如果您安装了XCode,则上述段落不适用。如果您尝试gem install pg,您将获得有关出现问题的其他信息。试试并发布结果。

关键是"你必须先安装开发工具。"这是一个强有力的暗示,你没有适当的编译器。该页面说XCode必须是v5.01或更高版本。