From: Rob Biedenharn Date: 2008-06-06T08:57:30+09:00 Subject: Re: RubyToC - Second Question It looks like the preamble is missing. The one in the rubyforge doc doesn't quite cut it since make doesn't find ruby.h (I'm sure a CFLAGS change would fix that). Here's a minimal set of changes that makes for a clean compile: + Add the first 3 lines * change the typecast on malloc to be str* * change long to int for main (prototype and definition) Perhaps you want to check out treetop or one of the other parsers as this project looks like it hasn't been touched in 2 years. -Rob #include #include typedef char * str; void arr_iterate(); int main(); void arr_iterate() { str * tstarr; tstarr = (str *) malloc(sizeof(str) * 3); tstarr[0] = "this is the first line"; tstarr[1] = "this is the second line"; tstarr[2] = "last line"; unsigned long index_line; for (index_line = 0; tstarr[index_line] != NULL; ++index_line) { str line = tstarr[index_line]; puts(line); } } int main() { arr_iterate(); return 0; } On Jun 5, 2008, at 7:15 PM, Philip Rhoades wrote: > People, > > > truby2c.rb: > > #!/usr/bin/ruby > > require 'rubygems' > require 'ruby_to_ansi_c' > > class MyTest > def arr_iterate > tstarr = [ 'this is the first line', 'this is the second line', > 'last line' ] > > tstarr.each { |line| > puts line > } > end > > def main > arr_iterate > return 0 > end > end > > result = RubyToAnsiC.translate_all_of MyTest > puts result > > > gives C output: > > void arr_iterate(); > long main(); > > void > arr_iterate() { > str * tstarr; > tstarr = (str) malloc(sizeof(str) * 3); > tstarr[0] = "this is the first line"; > tstarr[1] = "this is the second line"; > tstarr[2] = "last line"; > unsigned long index_line; > for (index_line = 0; tstarr[index_line] != NULL; ++index_line) { > str line = tstarr[index_line]; > puts(line); > } > } > > long > main() { > arr_iterate(); > return 0; > } > > > compiling with gcc gives: > > truby2c.c: In function 'arr_iterate': > truby2c.c:9: error: 'str' undeclared (first use in this function) > truby2c.c:9: error: (Each undeclared identifier is reported only once > truby2c.c:9: error: for each function it appears in.) > truby2c.c:9: error: 'tstarr' undeclared (first use in this function) > truby2c.c:10: error: expected ';' before 'malloc' > truby2c.c:16: error: expected ';' before 'line' > truby2c.c:17: error: 'line' undeclared (first use in this function) > truby2c.c: In function 'main': > truby2c.c:22: warning: return type of 'main' is not 'int' > > > - seems like some fundamental problems with such a little program? > > Thanks, > > Phil. > -- > Philip Rhoades > > Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) > GPO Box 3411 > Sydney NSW 2001 > Australia > E-mail: phil@pricom.com.au > Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com