我开始在C中开发ruby gem扩展,但我遇到了一个看似简单的问题。
我要做的是创建一个简单的gem扩展,它将在C中执行一些使用openmp的代码。
这是我的extconf.rb和我的c代码:
extconf.rb
require "mkmf"
extension_name = "visualize_helper"
dir_config("visualize_helper")
CONFIG["CC"] = "gcc-5"
$CXXFLAGS += ' -fopenmp '
$CFLAGS += ' -fopenmp '
have_library('gomp', "main")
create_makefile "visualize_helper/visualize_helper"
我需要更改编译器 CONFIG [" CC"] 因为OSX默认的gcc编译器是CLANG编译器,所以通过brew安装了gcc-5。
visualize_helper.c
#include <ruby.h>
#include <stdlib.h>
#include <omp.h>
static VALUE openmp_test(VALUE self, VALUE string) {
int nthreads, tid;
FILE *f = fopen("/tmp/open_mp", "w");
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
fprintf(f,"Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
fprintf(f,"Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
fclose(f);
return string;
}
// Main function called when the gem is loaded
void Init_visualize_helper(void) {
// Register the method openmp_test as a singleton for the class
rb_define_singleton_method(mVisualizeHelper, "openmp_test",openmp_test, 1 );
}
尝试编译时,在第一步创建Makefile:
ruby extconf.rb
我收到以下错误:
checking for Init_visualize_helper() in -lgomp... *** 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=/Users/rapha/.rvm/rubies/ruby-2.1.5/bin/ruby
--with-visualize_helper-dir
--without-visualize_helper-dir
--with-visualize_helper-include
--without-visualize_helper-include=${visualize_helper-dir}/include
--with-visualize_helper-lib
--without-visualize_helper-lib=${visualize_helper-dir}/lib
--with-gomplib
--without-gomplib
/Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:541:in `try_link0'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:556:in `try_link'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:742:in `try_func'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:973:in `block in have_library'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in postpone'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:321:in `open'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:321:in `open'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:347:in `postpone'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for'
from /Users/rapha/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:968:in `have_library'
from extconf.rb:7:in `<main>'
mkmf.log
"gcc -o conftest -I/Users/rapha/.rvm/rubies/ruby-2.1.5/include/ruby-2.1.0/x86_64-darwin14.0 -I/Users/rapha/.rvm/rubies/ruby-2.1.5/include/ruby-2.1.0/ruby/backward -I/Users/rapha/.rvm/rubies/ruby- 2.1.5/include/ruby-2.1.0 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE - D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 - I/Users/mpapis/.sm/pkg/active/include -fPIC -mmacosx-version-min=10.7 - pipe -fopenmp conftest.c -L. -L/Users/rapha/.rvm/rubies/ruby-2.1.5/lib -L. -L/Users/mpapis/.sm/pkg/active/lib -fPIC -Bstatic -fstack-protector -arch x86_64 -lruby-static -framework CoreFoundation -lpthread -ldl - lobjc "
ld: warning: directory not found for option '- L/Users/mpapis/.sm/pkg/active/lib'
ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5: return 0;
6: }
/* end */
我尝试过的事情
我眼前的三件事是:
我将我的Xcode版本更新为7.2.1,将我的RVM更新为1.27.0,我甚至尝试通过rvm编译ruby 2.1.5和2.3.0而不是使用二进制文件。
多一点信息
如果我删除 have_lib(&#39; gomp&#39;,X)这两行,我编译并生成makefile,如果检查makefile,我会看到:
CC = gcc-5
答案 0 :(得分:1)
要解决此问题,我最终在/ usr / local / bin / gcc上创建了一个链接到gcc-5:
ln -s /usr/local/bin/gcc-5 /usr/local/bin/gcc
并从extconf.rb中删除以下行:
CONFIG [“CC”] =“gcc-5”