在ruby C扩展gem[OSX]中需要lib(openmp)



我开始在C中开发ruby gem扩展,但我遇到了一个看起来很简单的问题。

我想做的是创建一个简单的gem扩展,它将使用openmp在C中执行一些代码。

这是我的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 = %dn", tid);
     /* Only master thread does this */
     if (tid == 0)
     {
       nthreads = omp_get_num_threads();
       fprintf(f,"Number of threads = %dn", 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 */

我尝试过的东西

我首先想到的三件事是:

  • &quot--用gomplib"
  • "您必须首先安装开发工具"
  • mkmf.log显示的是gcc(即CLANG),而不是我在extconf.rb中指定的gcc-5

我将Xcode版本更新到7.2.1,将RVM更新到1.27.0,甚至尝试通过RVM编译ruby 2.1.5和2.3.0,而不是使用二进制文件。

更多信息

如果我删除have_lib('comp',X)的两行,我编译并生成makefile,如果检查makefile,我会看到:

CC = gcc-5 

为了解决这个问题,我最终在/usr/local/bin/gcc上创建了一个到gcc-5:的链接

 ln -s /usr/local/bin/gcc-5 /usr/local/bin/gcc

并从extconf.rb中删除以下行:

CONFIG["CC"]="gcc-5"

最新更新