当我包含'<algorithm>'和'<vector>'时崩溃RubyInline。

Crash RubyInline when I include '<algorithm>' and '<vector>'

本文关键字:gt lt vector 崩溃 RubyInline algorithm 包含      更新时间:2023-10-16

我不明白为什么RubyInline崩溃了。

class CPPCode
    inline do |builder|
        builder.include '<algorithm>'
        builder.include '<vector>'
        builder.c 'int test(){return 1;}'
    end
end

错误:

执行"gcc -shared -fPIC -O3 -fno-fast-math -ggdb3 -wall -wextra -wno-unused-parameter -Wno-括号 -Wno-long-long -Wno-missing field-initializers -wunused-variable -wpointer-arith -wwrite-string -wdeclaration-afterstatement -wimplicit-function-declaration-declarations -wdeprecated-declarations -Wno-packed-bitfield-compat -fPIC -L. -fstack-protector -rdynamic -wl,-export-dynamic -I/home/lionzxy/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0 -I/home/lionzxy/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/x86_64-linux -I/home/lionzxy/.rvm/rubies/ruby-2.3.0/include -L/home/lionzxy/.rvm/rubies/ruby-2.3.0/lib -o \"/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.so\" \"/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c\" ": PID 24454 出口 1 重命名/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c 至/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c.bad

生成的C++文件:

#include "ruby.h"
#include <algorithm>
#include <vector>
# line 58 "/home/lionzxy/RubymineProjects/app/models/book.rb"
static VALUE test(VALUE self) {
return INT2FIX(1);}

#ifdef __cplusplus
extern "C" {
#endif
void Init_Inline_Book__CPPCode_098f6bcd4621d373cade4e832627b4f6() {
    VALUE c = rb_cObject;
    c = rb_const_get(c, rb_intern("Book"));
    c = rb_const_get(c, rb_intern("CPPCode"));
    rb_define_method(c, "test", (VALUE(*)(ANYARGS))test, 0);
}
#ifdef __cplusplus
}
#endif

您正在尝试使用C++代码,但是您正在运行gcc,没有任何标志来告诉它它是C++代码。

在第一个文档页面上,我们看到一个有关如何使用C++代码的示例:

require 'inline'
class MyTest
    inline(:C) do |builder|
        builder.include '<iostream>'
        builder.add_compile_flags '-x c++', '-lstdc++'
        builder.c '
            void hello(int i) {
                while (i-- > 0) {
                    std::cout << "hello" << std::endl;
                }
            }'
    end
end
t = MyTest.new()
t.hello(3)

这里的"诀窍"是:

builder.add_compile_flags '-x c++', '-lstdc++'

这告诉gcc这是C++代码。