compiling C and fortran files in MATLAB mex

Hi i have a standalone fortran code and i have written a mex interface function to access it from MATLAB. My fortran code has one .c file named adwrite.c. When i try to compile it in MATLAB i get an error "C:\PROGRA~1\MATLAB\R2012A\BIN\MEX.PL: Error: 'adwrite.c' not found. ". I have intel fortran compiler in my system. Can some one help me with this problem. Thank You in advance

 채택된 답변

James Tursa
James Tursa 2014년 11월 15일
편집: James Tursa 2014년 11월 15일

0 개 추천

First compile adwrite.c by itself with a C compiler to generate an object file (e.g., adwrite.obj on Windows). If you are doing it from the MATLAB command line simply add the -c option to request compile only without linking. Then compile your Fortran mex routine including the adwrite.obj file on the command (*not* the adwrite.c). Be sure adwrite.obj is in the current default directory.

댓글 수: 6

Dear James, I have a similar situation where I need to call a Fortran function from C mex function.
Do you know how to use a C-Fortran interface for C mex functions in MatLab?
We are using MatLab 7.10, where it has intalled its ‘Lcc-win32 C 2.4.1’ C compiler and ‘Gnumex v2.06’ Fortran compiler. We would like to use the code written in Fortran90/95 but using the C mexfuntion of MatLab, because we have experience in C and not in Fortran. Then we should use the Fortran functions compiling previously with Gnumex and obtaining the corresponding .obj file. This experience has been tested using 2 files written IN FORTRAN, one (add.f90) for the function and the other (adding.f90) for Fortran mexfuntion:
-------add.f90----------------
subroutine ADD(ad, a, b)
implicit none
double precision :: ad, a, b
intent(in) :: a, b
intent(out) :: ad
ad=a+b
end subroutine ADD
------------------------------
-------adding.f90---------------------------------
module addsub_mod ! test module for gnumex and g95
use mexinterface
end module addsub_mod
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
use addsub_mod
implicit none
integer :: nlhs, nrhs, plhs(nlhs), prhs(nrhs)
double precision, pointer :: addp, ap, bp
plhs(1) = mxCreateDoubleMatrix(1,1,0)
plhs(2) = mxCreateDoubleMatrix(1,1,0)
call c_f_pointer(mxgetpr(prhs(1)), ap)
call c_f_pointer(mxgetpr(prhs(2)), bp)
call c_f_pointer(mxgetpr(plhs(1)), addp)
call ADD(addp, ap, bp)
end subroutine mexfunction
----------------------------------------------------
Then executin the following steps, >> mex -c add.f90 >> mex add.obj adding.f90 >> result=add(3,4) result = 7 where we obtain the desired result.
Now, we would like to use the add.obj file but using the following C mexfunction interface (adding.c),
-------adding.c---------------------------------
#include "mex.h"
subroutine ADD(result, a, b); // Declaration of the function writen in Fortran
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *a, *b; // inputs
double *result; // output
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
a = mxGetPr(prhs[0]); b = mxGetPr(prhs[1]);
result = mxGetPr(plhs[0]);
ADD(result, a, b); // Call the function written in Fortran
}
----------------------------------------------------
then we choose to C compiler with >> mex –setup
and execute, >> mex add.obj adding.c
but we obtain the following errors:
lcc preprocessor warning: adding.c:13 No newline at end of file Error adding.c: 2 syntax error; found `ADD' expecting `;' Error adding.c: 2 extraneous old-style parameter list 2 errors, 1 warnings C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Compile of 'adding.c' failed.
Do you know how to do the Declaration and Call lines for Fortran function?
Thanks,
Patxi Alkorta Egiguren
James Tursa
James Tursa 2015년 4월 13일
편집: James Tursa 2015년 4월 13일
You should really open a new Question for this rather than hijack an existing Question. But one obvious error in the C mexFunction interface is the "subroutine" line, which is Fortran syntax, not C syntax. So try this instead:
void ADD(double *, double *, double *); // Declaration of the function writen in Fortran
I will make an additional observation about your adding.f90 code. The following line looks erroneous and may crash MATLAB. You are writing into the 2nd spot in the plhs array when there is likely only one spot available since the user likely called the function with only one output. Get rid of this line.
plhs(2) = mxCreateDoubleMatrix(1,1,0)
Patxi
Patxi 2015년 4월 13일
편집: Patxi 2015년 4월 13일
Dear James Tursa, thank you very much for your answer. I have done your suggested change but it produces another error >> mex add.obj adding.c
cc1.exe: warning: command line option '-fno-underscoring' is valid for Fortran but not for C [enabled by default] Cannot export mexfunction: symbol not defined D:\DOCUME~1\ISPALEGP\LOCALS~1\TEMP\MEX_ZH~1\adding.obj:adding.c:(.text+0x62): undefined reference to `ADD' collect2.exe: error: ld returned 1 exit status link command: gfortran -shared C:\Fortran\gnumex\examples\ADIBID~1\gfortmex.def -o add.mexw32 -LC:\Fortran\gnumex\examples\ADIBID~1 -s add.obj D:\DOCUME~1\ISPALEGP\LOCALS~1\TEMP\MEX_ZH~1\adding.obj -lflibmx -lflibmex -lflibmat
C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Link of 'add.mexw32' failed.
??? Error using ==> mex at 222 Unable to complete successfully.
Could you help me?
Regards, Patxi Alkorta
James Tursa
James Tursa 2015년 4월 13일
편집: James Tursa 2015년 4월 13일
I have no experience with the particular compilers you are using, but try reversing the order of the arguments. E.g.,
mex adding.c add.obj
If that doesn't work, what exact error message do you get when you try to compile the Fortran gateway routine without the add.obj file in the command line? E.g., do this:
mex adding.f90
You should get a link error about not finding the ADD routine. What we are looking for is the exact spelling of the ADD routine that it is looking for (any name mangling with underscores, etc.) so that we know exactly what the C mexFunction should be using for a function name. Then compile the C gateway function to see what it is looking for (i.e., force a link error again):
mex adding.c
By comparing the two maybe you can determine how to change things to get the C routine to call the exact Fortran function by the appropriate name.
Patxi
Patxi 2015년 4월 13일
I have done the first command and, unfortunatelly, it produces the same error. The second command produces the same error using C or Fortran compiler:
D:\DOCUME~1\ISPALEGP\LOCALS~1\TEMP\MEX_FL~1\adding.obj:adding.f90:(.text+0x5e): undefined reference to `add' collect2.exe: error: ld returned 1 exit status link command: gfortran -shared C:\Fortran\gnumex\examples\ADIBID~1\gfortmex.def -o adding.mexw32 -LC:\Fortran\gnumex\examples\ADIBID~1 -s D:\DOCUME~1\ISPALEGP\LOCALS~1\TEMP\MEX_FL~1\adding.obj -lflibmx -lflibmex -lflibmat C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Link of 'adding.mexw32' failed.
and editing add.obj I can see how appears '_add', but replacing this name by ADD in both add.f90 and adding.f90, and compiling with Fortran compiler, it produces a syntax error:
>> mex adding.f90 adding.f90:14.6: call _add(addp, ap, bp) 1 Error: Syntax error in CALL statement at (1) C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Compile of 'adding.f90' failed.
then, in order to solve last error, I've replaced add by ADD, and it produces another error:
>> mex add.obj adding.c File add.obj contains unknown section /4. .text section assumed File add.obj contains unknown section /15. .text section assumed Relocation out of range!
Link failed. Removing add.mexw32 C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Link of 'add.mexw32' failed. ??? Error using ==> mex at 222 Unable to complete successfully.
Thanks,
Patxi
James Tursa
James Tursa 2015년 4월 13일
Maybe we need to back up a step. What version of MATLAB are you using? Later versions automatically select the C or Fortran compiler depending on the file extensions of the argument list. But earlier versions of MATLAB require you to manually switch compilers between mex commands. Are you sure you are compiling the Fortran routine with the Fortran compiler, and the C routine with the C compiler, and not vice-versa?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

질문:

2014년 11월 15일

댓글:

2015년 4월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by