How to call a compiled fortran exe file in a deployed Matlab exe in Linux?

9 views (last 30 days)
(Or maybe I should ask How to compile a totally independent excutable Fortran file in Linux....)
Hi guys, I am trying to use a deployed Matlab exe file in Linux, and in the middle of my matlab code I want to call a .exe file compiled by Fortran.
I have tested that the fortran-compiled binary exe file is finely running in Linux and get my results, either calling it in Matlab by
system(read_results.exe)
or
current_folder=pwd;
readf_results_path=fullfile(current_folder,'read_results.exe' );
[status, results] = system ( readf_results_path );
Both ways work. And when I deployed the matlab code to binary file on my Windows PC, it works fine.
But when I deployed the Matlab code, and call the deployed binary Matlab code by the following command:
./matlab_code.sh /cm/shared/apps/Matlab-R2019a
The Fortran .exe file cannot be called correctly(in the binary file, the other parts are fine) and gives the following message:
Error using Function_1 (line 30)
Possible reason:/newhome/user/opt/read_results.exe: error while loading shared libraries: libgfortran.so.5: cannot open shared object file: No such file or directory
Error in matlab_code (line 264)
I guess this problem shoulbe be relatived with the environment variables or something like that. Here is my guess:
-When I call it in deployed binary file on Windows, the Fortran compiled file is independent of any libraries, so it works;
-When I call it by Matlab code in Linux, because I have loaded GCC in my bash, so it works;
-When I call it in deployed binary file in Linux, the compiled Fortran execuatable file need a dependent library (libgfortran.so.5), so it geives the error.
I don't know how to add those libraries to a deployed Matlab code, or how to compile the Fortran code to a totally independent binary file in Linux, just like when you chose Realese and Mutilthreads in a Visual Studio environment to make it capable to run on any Windows . Could anyone please help me with it? Thank you guys very much!!!!
( The reason I need to call a .exe file is I really cann't compile it to a DLL...So I have to use it in such a terrible way. )

Answers (2)

Image Analyst
Image Analyst on 4 May 2020
Did you try system()?
system(pathNameToFortranExecutable);

Image Analyst
Image Analyst on 5 May 2020
Did you try giving the full path?
system(read_results.exe)
will not work, nor will
system('read_results.exe');
because it tries to look in the current folder, /newhome/user/opt/.
You will need to prepend the folder:
system("/cm/shared/apps/Matlab-R2019a/read_results.exe")
or wherever it is. This is because the actual MATLAB executable is not where you think it is. It's not where the executable is - it's actually in some secret special folder. Like in Windows, it's not under c:\program Files\My Matlab program" or wherever you installed it, though there is an executable there. It's actually in "c:/user/yourname/apps/local/blah/blah/blah" ro some weird place. Take the attached function and call it in your startup code. Print out the actual program folder and see where it is. Anyway, that's why you'll need to give the full path of your executable. You might be able to get away with not doing that if it's in your current/startup folder and you attach/embed it to the executable with the -a option when you call mcc to compile it.
  1 Comment
zexi wang
zexi wang on 5 May 2020
Thanks for you patient explaination. I have tried the fullpath but it gives the same error message as directly calling the name of .exe. Both way work on my Windows PC(Matlab code and deployed binary file), both way work in the Linux, but niether of them works in a deployed binary file in Linux. That's really weried.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!