Hello, I forgot to tell you that I am using Linux OS. And I can?t find directory "src/nmath/standalone". I will send you the test code I am using and the whole operation process. [credsim at confsys ~/src]$ gcc test1.c -o test1 -lRmath test1.c: In function `main': test1.c:18: warning: assignment makes pointer from integer without a cast test1.c:19: warning: assignment makes pointer from integer without a cast test1.c:41: warning: assignment makes pointer from integer without a cast test1.c: At top level: test1.c:55: warning: type mismatch with previous implicit declaration test1.c:18: warning: previous implicit declaration of `Allocate_Memory_2D' test1.c:55: warning: `Allocate_Memory_2D' was previously implicitly declared to return `int' /usr/bin/ld: cannot find -lRmath collect2: ld returned 1 exit status "test1.c" code is as follow: #define MATHLIB_STANDALONE #include <math.h> #include <stdlib.h> #include <stdio.h> #include </usr/local/lib/R/include/Rmath.h> main() { double **x, **y, **x1, **y1, valin; int i,j,I,J; I=3; J=3; x=Allocate_Memory_2D( I, J, x1); y=Allocate_Memory_2D( I, J, y1); FILE *in_file; /* input x value from file data_2Dx.txt */ in_file=fopen("data_2Dx.txt","r"); if (in_file==NULL) {/*Test for error*/ fprintf(stderr,"Error:Unable to input file from 'data_2Dx.txt'\n"); exit(8); } for( i=0;i<I; i++) for (j=0;j<J;j++) { fscanf(in_file, "%lf\n", &valin, stdin);/* read a single double value in */ x[i][j]=valin; valin=0.0; } fclose(in_file); y=solve(x); for (i=0;i<I;i++) for (j=0;j<J;j++) { printf ("y[%d][%d]=%lf\n", i, j, y[i][j]); } } double **Allocate_Memory_2D( int I, int J, double **W) { int i; W=(double **)malloc(I*sizeof (double *)); if(!W) printf("It is out of memory. Allocation failed."); for (i=0;i<I;i++) { W[i]=(double *)malloc(J*sizeof(double)); if(!W[i]) printf("It is out of memory. Allocation failed."); } return (W); } Looking forward to your early reply! Thanks! Maggie Wang
Maggie, Are you sure you actually _have_ a library libRmath available to your linker? Below is a full log of how this works on any given Debian system for which we provide the library in a package r-mathlib. Once that is installed, you should be set. Hth, Dirk edd at basebud:/usr/share/doc/r-mathlib/examples> cat test.c /* * Mathlib : A C Library of Special Functions * Copyright (C) 2000 The R Development Core Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #define MATHLIB_STANDALONE 1 #include <Rmath.h> int main() { /* something to force the library to be included */ qnorm(0.7, 0.0, 1.0, 0, 0); return 0; } edd at basebud:/usr/share/doc/r-mathlib/examples> gcc -Wall -o /tmp/nmath_test test.c -lRmath -lm edd at basebud:/usr/share/doc/r-mathlib/examples> ldd /tmp/nmath_test libRmath.so.1 => /usr/lib/libRmath.so.1 (0x40028000) libm.so.6 => /lib/libm.so.6 (0x40047000) libc.so.6 => /lib/libc.so.6 (0x40069000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) edd at basebud:/usr/share/doc/r-mathlib/examples> -- The relationship between the computed price and reality is as yet unknown. -- From the pac(8) manual page
Thanks every expert! I have fixed the problem that I can use R library in c code right now. However, it still has problem that I can not compile the c code. I attach the operation process to this letter. Who is so kind to help me find where is the problem? Thnaks in advance! [credsim at confsys ~/src]$ gcc test1.c -o test1 -lRmath -lm /tmp/ccYbnKy1.o(.text+0xd0): In function `main': : undefined reference to `dim' /tmp/ccYbnKy1.o(.text+0xdf): In function `main': : undefined reference to `c' /tmp/ccYbnKy1.o(.text+0xee): In function `main': : undefined reference to `solve' collect2: ld returned 1 exit status test1.c code is as follow: [credsim at confsys ~/src]$ vi test1.c #define MATHLIB_STANDALONE #include <math.h> #include <stdlib.h> #include <stdio.h> #include </usr/local/lib/R/include/Rmath.h> main() { int i,j,I,J,n; I=3; J=3; n=I*J; double valin; double x2[9]; FILE *in_file; /* input x value from file data_2Dx.txt */ in_file=fopen("data_2Dx.txt","r"); if (in_file==NULL) {/*Test for error*/ fprintf(stderr,"Error:Unable to input file from 'data_2Dx.txt'\n"); exit(8); } for( i=0;i<n;i++) /* for (j=0;j<J;j++)*/ { fscanf(in_file, "%lf\n", &valin, stdin);/* read a single double value in */ x2[i]=valin; valin=0.0; } fclose(in_file); dim(x2)<-c(3,3); solve(x2); return (0); }