compiling C and fortran files in MATLAB mex

1 view (last 30 days)
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

Accepted Answer

James Tursa
James Tursa on 15 Nov 2014
Edited: James Tursa on 15 Nov 2014
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 Comments
Patxi
Patxi on 13 Apr 2015
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 on 13 Apr 2015
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!