Chapitre 6. Programmation en R
275
Linux
Sous Linux, utilisez :
gfortran -c random.f -o randomf.o -fPIC
gcc -c random.c -o randomc.o -I"/usr/lib/R/include" -fPIC
gfortran -shared randomf.o randomc.o -o random.so
Vous pouvez maintenant appeler votre code depuis R au moyen des instructions
suivantes :
> dyn.load(paste("random",.Platform$dynlib.ext,sep=""))
> .Fortran("random", as.double(1), as.double(1))
[[1]]
[1] 2.100142
[[2]]
[1] 0.9682872
Il est ´ egalement possible d’appeler des fonctions Fortran depuis un code
C/C++, en se servant des instructions suivantes :
F77_NAME(name) pour d´ eclarer une routine Fortran en C
F77_CALL(name) pour appeler une routine Fortran depuis C
F77_COMDECL(name) pour d´ eclarer un bloc COMMON FORTRAN en C
F77_COM(name) pour acc´ eder ` a un bloc COMMON FORTRAN depuis C
Voyons ceci sur un petit exemple (avec du Fortran77 pour changer). Enregistrez le code ci-dessous dans un fichier nomm´ e combnCF.cpp :
1 # include
2 # include < Rmath .h >
3 extern "C" {
4 void combnCF ( int * combmat , int * n , int * m) {
5 // Attention ! Il ne faut pas de majuscules dans le nom
6 // de la sous - routine .
7 void F77 _ NAME ( combnf )( int * combmat , int * n , int * m );
8 F77 _ CALL ( combnf )( combmat ,n ,m );
9 }
10 }
Tapez ensuite les instructions suivantes dans une fenˆ etre de commandes
MS-DOS afin de cr´ eer la librairie qui sera appel´ ee depuis R :
g++ -c combnCF.cpp -o combnCF.o -I"C:\Program Files\R\R-3.1.0\include"
gfortran -c combn.f90 -o combn.o
g++ -shared -o combnCF.dll combnCF.o combn.o ^
-L"C:\Program Files\R\R-3.1.0\bin\i386" -lR
Précédent

- 308/701

Suivant