Call Matlab from Intel Fortran (Linux)

霸气de小男生 提交于 2019-12-24 12:45:21

问题


I am trying to integrate a Matlab program I wrote into some Fortran code. I tried to follow the example Mathworks provides. But I can't get it to compile because I can't find the header files it requests.

Does anyone know of an example of someone getting it to work on Linux using an Intel compiler. I think that might be part of the problem because Matlab only supports GNU Fortran on Linux.

And I realize this is a simple question, I just don't understand how to do anything in compiling more complicated than including multiple files with defined paths.


回答1:


Disclaimer: I'm currently using OS X so I can only provide output from OS X but everything should transfer easily over to Linux due to the Unix base. I also don't have the Intel Fortran compiler on OS X (only the C/C++ compiler).


Note: You will need to substitute the paths I use for the correct paths on your system depending on your MATLAB installation directory.


This issue isn't specific to the Intel Compiler, I also receive errors with the GCC Fortran compiler.

$ gfortran fengdemo.F 
fengdemo.F:1:0:

 #include "fintrf.h"
 ^
Fatal Error: fintrf.h: No such file or directory
compilation terminated.

You can use the Unix locate command to find files.

$ locate fintrf.h
/Applications/Matlab R2014a.app/extern/include/fintrf.h

In the directory where fengdemo.F is we can then pass the correct directory in using the -I option

-I../../include/

However, this produces linking errors as we haven't specified where the libraries for fintrf.h can be found. We can do this with the -L option (you will need to replace maci64 with the correct option for Linux - I can't remember it off the top of my head but you should be able to see it in the bin directory)

-L../../../bin/maci64/

Now we need to tell it what libraries to use with -leng -lmx and so the completed command is

$ ifort fengdemo.F -I../../include/ -L../../../bin/maci64/ -leng -lmx

and it should compile correctly.

We aren't finished yet though as it won't execute. We need to set up our PATH and DYLD_LIBRARY_PATH environment variables correctly. Specifically we need to add the bin and bin/maci64 directories of our MATLAB installation to PATH

$ export PATH=$PATH:/Applications/Matlab\ R2014a.app/bin/maci64:/Applications/Matlab\ R2014a.app/bin

and the bin/maci64/ and sys/os/maci64/ to DYLD_LIBRARY_PATH

$ export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/Applications/Matlab\ R2014a.app/bin/maci64/:/Applications/Matlab\ R2014a.app/sys/os/maci64/

Note: On Linux DYLD_LIBRARY_PATH should be LD_LIBRARY_PATH. Thanks to Vladimir F for correcting me.

Now you can execute the program using

$ ./a.out


来源:https://stackoverflow.com/questions/32769469/call-matlab-from-intel-fortran-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!