- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I tried to call MATLAB from visual Fortran, but failed. Could anyone tell me what to do to fix it?
Thanks!
The environment is: 64bit win10; 64bit MATLAB2018a; Intel parallel studio XE 2017fortran + Microsoft visual studio 2017; Debug x64.
The output information in visual studio is:
1>------ Build started: Project: Console2, Configuration: Debug x64 ------
1>Linking...
1>libifcoremdd.lib(for_main.obj) : error LNK2019: unresolved external symbol MAIN__ referenced in function main
1>x64\Debug\Console2.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build log written to "file://C:\Users\Rui\FORTRAN\Console2\Console2\x64\Debug\BuildLog.htm"
1>Console2 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The code in Fortran is actually an example (passstr.f) supplied by MATLAB
#include "fintrf.h" C===================================================================== #if 0 C C passstr.F C .F file needs to be preprocessed to generate .for equivalent C #endif C C passstr.F is an example for illustrating passing a character C matrix from FORTRAN to MATLAB. C C It passes a string array/character matrix into MATLAB as output C arguments rather than placing it directly into the workspace. C C Copyright 1984-2009 The MathWorks, Inc. C C===================================================================== C Gateway routine subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C Declarations implicit none
C mexFunction arguments: mwPointer plhs(*), prhs(*) integer nlhs, nrhs
C Function declarations: mwPointer mxCreateString integer mexCallMATLAB
C Pointers to input/output mxArrays: mwPointer input(1)
integer status mwSize i, m, n character*75 thestring character*15 string(5)
C----------------------------------------------------------------------- C Create the string to be passed into MATLAB. string(1) = 'MATLAB ' string(2) = 'The Scientific ' string(3) = 'Computing ' string(4) = 'Environment ' string(5) = ' by TMW, Inc.'
C Concatenate the set of 5 strings into a long string. thestring = string(1) do 10 i=2,5 thestring = thestring(:((i-1)*15)) // string(i) 10 continue
C Create the string matrix to be passed into MATLAB. input(1) = mxCreateString(thestring)
C Set the matrix size to be M=15 and N=5. m = 15 call mxSetM(input(1), m) n = 5 call mxSetN(input(1), n)
C Transpose the resulting matrix in MATLAB because C Fortran stores arrays as column major. status = mexCallMATLAB(1, plhs, 1, input, 'transpose')
C Cleanup the un-freed memory after calling mexCallMATLAB. call mxDestroyArray(input(1))
return end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Start over with a new project, and this time select "Static library" instead of "Console Application". When you selected Console Application, you told it you wanted to create a standalone Fortran executable program, but all you have is a subroutine. You want a library instead to be linked with Matlab.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Start over with a new project, and this time select "Static library" instead of "Console Application". When you selected Console Application, you told it you wanted to create a standalone Fortran executable program, but all you have is a subroutine. You want a library instead to be linked with Matlab.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Also - see the Matlab documentation for how to integrate a Fortran procedure into your Matlab environment. You can't just "call Matlab" independently.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Rui Yang, it helps to understand the role of the Mex-file before building it. Your statement, "I tried to call MATLAB from Visual Fortran", is incorrect. The purpose of this example is to create a Mex file (a DLL specific to Matlab) from Fortran source whose function is to send some strings to Matlab; but this is done when Matlab calls the Mex function, not the other way around.
Since the example is part of the standard Matlab distribution, it is well-documented, e.g., see http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/matlab_external/ch04cre7.html .
This is how I built the Mex file on Windows for Matlab 2013b (x64). I opened an IFort 64-bit command window, and added the Matlab directory ...\extern\include to INCLUDE and ...\extern\lib\win64\microsoft to LIB. Next, I built the DLL with the command
ifort /dll /fpp passstr.F libmx.lib libmex.lib /link /export:MEXFUNCTION /out:passstr.mexw64
Then, I opened Matlab, changed to the directory containing the just-produced Mex file, and typed "passstr", and saw the results in Matlab.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much!
mecej4 wrote:
Rui Yang, it helps to understand the role of the Mex-file before building it. Your statement, "I tried to call MATLAB from Visual Fortran", is incorrect. The purpose of this example is to create a Mex file (a DLL specific to Matlab) from Fortran source whose function is to send some strings to Matlab; but this is done when Matlab calls the Mex function, not the other way around.
Since the example is part of the standard Matlab distribution, it is well-documented, e.g., see http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/matlab_ext... .
This is how I built the Mex file on Windows for Matlab 2013b (x64). I opened an IFort 64-bit command window, and added the Matlab directory ...\extern\include to INCLUDE and ...\extern\lib\win64\microsoft to LIB. Next, I built the DLL with the command
ifort /dll /fpp passstr.F libmx.lib libmex.lib /link /export:MEXFUNCTION /out:passstr.mexw64Then, I opened Matlab, changed to the directory containing the just-produced Mex file, and typed "passstr", and saw the results in Matlab.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page