[Rd] SHLIB works but inline compilation does not
Sharpie
chuck at sharpsteen.net
Wed Apr 14 04:02:33 CEST 2010
Jeff Brown wrote:
>
> Hi,
>
> I appear to be able to compile, load and call shared objects using SHLIB
> and .Call:
>
>> code <- '#include <R.h>\n #include <Rdefines.h>\n SEXP f(){\n return
>> R_NilValue ; }'
>> writeLines( code, "test.c" )
>> system( "R CMD SHLIB test.c" )
> gcc -arch i386 -std=gnu99
> -I/Library/Frameworks/R.framework/Resources/include
> -I/Library/Frameworks/R.framework/Resources/include/i386
> -I/usr/local/include -fPIC -g -O2 -c test.c -o test.o
> gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
> -mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module
> -multiply_defined suppress -L/usr/local/lib -o test.so test.o
> -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
> -Wl,CoreFoundation
>> dyn.load( "test.so" )
>> .Call( "f" )
> NULL
>
> The above follows Romaine-Francois 3's suggestion here:
> http://n4.nabble.com/I-can-t-run-the-example-shown-in-the-inline-package-td1774328.html#a1778838
>
> However, when I try to use the "inline" package, I can't get it to work:
>
>> code <- "
> + int i, j, nab = *na + *nb - 1;
> + for(i = 0; i < nab; i++)
> + ab[i] = 0.0;
> + for(i = 0; i < *na; i++) {
> + for(j = 0; j < *nb; j++)
> + ab[i + j] += a[i] * b[j];
> + }"
>> fun <- cfunction(
> + signature(a="numeric", na="numeric", b="numeric", nb="numeric",
> ab="numeric"),
> + code,
> + language="C",
> + convention=".C" )
>
> ERROR(s) during compilation: source code errors or compiler configuration
> errors!
>
> Program source:
> 1: #include <R.h>
> 2:
> 3:
> 4: void file4431b782 ( double * a, double * na, double * b, double * nb,
> double * ab ) {
> 5:
> 6: int i, j, nab = *na + *nb - 1;
> 7: for(i = 0; i < nab; i++)
> 8: ab[i] = 0.0;
> 9: for(i = 0; i < *na; i++) {
> 10: for(j = 0; j < *nb; j++)
> 11: ab[i + j] += a[i] * b[j];
> 12: }
> 13: }
> Error in compileCode(f, code, language, verbose) :
> Compilation ERROR, function(s)/method(s) not created!
>
> The C function above is the one from "Writing R Extensions", p. 79; the
> suggestion for how to compile it inline is from Dirk Eddelbuettel, at this
> thread:
> http://n4.nabble.com/Getting-started-with-C-td1837912.html#a1837912
>
> R says it's either a source code or configuration error. I got the code
> from "Writing R Extensions", and Dirk tried it out too. But if it's a
> compilation error, then shouldn't the first method (with SHLIB) not work
> either?
>
> I realize I could stick to the SHLIB method, which Charlie ("The Sharpie")
> Sharpsteen wrote up in fantastic detail here:
> http://n4.nabble.com/Writing-own-simulation-function-in-C-td1580190.html#a1580423
> But there's so much more stuff I'll need to understand if I do it that way
> ...
>
> I use an Intel MacBook Pro with OS X 10.6.2, XCode 3.2.1, gcc 4.2.1.
>
> Thanks,
> Jeff
>
>
I could not reproduce this problem on my computer, OS X 10.6.3, gcc 4.2.1, R
2.10.1, inline 0.3.4:
code <- "
int i, j, nab = *na + *nb - 1;
for(i = 0; i < nab; i++)
ab[i] = 0.0;
for(i = 0; i < *na; i++) {
for(j = 0; j < *nb; j++)
ab[i + j] += a[i] * b[j];
}"
fun <- cfunction(
signature(a="numeric", na="numeric", b="numeric", nb="numeric",
ab="numeric"),
code,
language="C",
convention=".C" )
fun( 1:3, 3, 4:6, 3, double(5) )
$a
[1] 1 2 3
$na
[1] 3
$b
[1] 4 5 6
$nb
[1] 3
$ab
[1] 4 13 28 27 18
The only thing I can think of is that you may have a newline issue going on.
Here's the dump of the code object:
dput(code)
" \nint i, j, nab = *na + *nb - 1; \nfor(i = 0; i < nab; i++) \n ab[i] =
0.0; \nfor(i = 0; i < *na; i++) { \n for(j = 0; j < *nb; j++) \n ab[i + j]
+= a[i] * b[j]; \n}"
Do you have newlines, \n, that are in different places? An easy way to
check may be to copy the results of running dput() on your code object into
one file, mine into another and running diff.
Hope this helps!
-Charlie
-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://n4.nabble.com/SHLIB-works-but-inline-compilation-does-not-tp1839090p1839237.html
Sent from the R devel mailing list archive at Nabble.com.
More information about the R-devel
mailing list