How should I mex a fortran code(main function) that calls other fortran codes(sub function) that in different files
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to mex a .F90 code that calls multiple fortran modules (I understand these to be sub functions) saved in a different file. Based on a limited understanding I obtained from: https://www.mathworks.com/matlabcentral/fileexchange/25934-fortran-95-interface-to-matlab-api-with-extras-
I used
mex -c module1.f90
for all modules. This creates two files for each of the module, one is a .obj file and the other is .mod file. After that I tried to mex the main function using
mex mainfnc.F90
I then get an error:
Creating library mainfnc.lib and object mainfnc.exp
mainfnc.obj : error LNK2019: unresolved external symbol MODULE1_mp_SETOPTION referenced in function MEXFUNCTION
mainfnc.obj : error LNK2019: unresolved external symbol MODULE2_MOD_mp_LRHB referenced in function MEXFUNCTION
mainfnc.mexw64 : fatal error LNK1120: 2 unresolved externals
If I try to mex using the following code:
mex mainfnc.F90 module1.f90 module2.f90 ....
I get the following errors:
Error using mex
Creating library LRHB.lib and object LRHB.exp
module1.obj : error LNK2019: unresolved external symbol DCOPY referenced in function MODULE1_mp_DELETEROWS
module2.obj : error LNK2001: unresolved external symbol DCOPY
module3.obj : error LNK2001: unresolved external symbol DCOPY
I would like to know which syntax is right and if I need to do some preprocessing of .f90 files before trying to mex the mainfnc.
Any help is appreciated. Thanks!
0 Kommentare
Antworten (1)
James Tursa
am 7 Apr. 2017
Bearbeitet: James Tursa
am 7 Apr. 2017
The modules need to be compiled first so that the interface .mod files are available for the downstream compiling. This can be accomplished in two ways. Either include the module source code at the front end of your source code, or compile the modules separately first and just use the module interface in your source code. You are doing the latter, which is fine.
After compiling the module separately (to get the .mod files), you simply need to include the associated module .obj files in your mex command. E.g.,
mex mainfnc.F90 module1.obj module2.obj ....
When mainfnc.F90 gets compiled and the compiler sees the "use module1" line, it will look for the module1.mod file to use for the interface. Just be sure that you use the module interfaces in your source code as the first lines after the subroutine line in mainfnc.F90. E.g.,
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
use module1
use module2
implicit none
:
etc
All that being said, what you did also would have sort of worked, but not in the way you probably thought. E.g., this command:
mex mainfnc.F90 module1.f90 module2.f90 ....
What the above does is compile mainfnc.F90 first, using the currently existing module1.mod and module2.mod files that happen to be out there. Then it will compile module1.f90 and module2.f90, creating new versions of module1.mod and module2.mod that were not used for the compile of the main function. There is a problem here. If you were to edit module1.f90 or module2.f90 (changing the interface) and then issue that compile command, the old interface files would be used, leading to errors that you would probably scratch your head over. You would double check your source code and see that everything looks OK, but the compile would not work. Bottom line is you need to make sure those module files get compiled first before you compile any code that uses them. Doing it in steps like you are doing is a good way of ensuring this. My advice is to NEVER list module source code on the same line as your own source code. Always compile them separately first (or include them at the top of your source code).
Your current DCOPY error is simply stating that you are missing the object code for the DCOPY routine in your mex command. You need to add another object file or library file that contains the DCOPY routine to your mex command line. I presume this is the BLAS routine DCOPY? If so, you need to include the BLAS object library file name as part of your mex command line. E.g., maybe the libmwblas.lib file under extern\lib\win64\microsoft will work for Fortran?
1 Kommentar
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!