Compiler Preprocessor Stringizing to Add Quotes

OK, carrying on w/ the idea of linking w/ GFORTRAN, I started with one function to see if could build the C binding. It works to build, up to a point.
The F90 source file is where the prototype is taken from the C syntax in the mex library documentation
#include "fintrf.h"
module MEXFUNCTIONS
interface
subroutine mexerrmsgidandtxt(errorID, errorMsg) bind(C,name=mexerrmsgidandtxt)
use iso_c_binding, only: c_char
character(kind=c_char) :: errorID(*), errorMsg(*)
end subroutine
end interface
end module
Compiling this and looking at the output of the preprocessor, the above goes thru the preprocessor step with no errors/warnings and produces
C:\GCC\bin\tmp> gfortran -E -Dm64 -DMATLAB_MEX_FILE -I C:\ML_R2020b\extern\include mexFunctionsModule.F90
# 1 "mexFunctionsModule.F90"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "mexFunctionsModule.F90"
# 1 "C:\\ML_R2020b\\extern\\include/fintrf.h" 1
# 9 "mexFunctionsModule.F90" 2
module MEXFUNCTIONS
interface
subroutine mexerrmsgidandtxt800(errorID, errorMsg) bind(C,name=mexerrmsgidandtxt800)
use iso_c_binding, only: c_char
character(kind=c_char) :: errorID(*), errorMsg(*)
end subroutine
end interface
end module
C:\GCC\bin\tmp>
where it can be observed the #define substitution of "mexerrmsgidandtxt800" for the source code string "mexerrmsgidandtxt".
Digging into the include file, it turns out the "800" suffix is one attached for the latest version and is set dynamically by some logic internal to the include file.
The above is fine excepting for and the reason for the Q?, the line
subroutine mexerrmsgidandtxt800(errorID, errorMsg) bind(C,name=mexerrmsgidandtxt800)
needs the entry name inside the bind() argument list to be a quoted string like
subroutine mexerrmsgidandtxt800(errorID, errorMsg) bind(C,name="mexerrmsgidandtxt800")
I have not been able to figure out a way to have the preprocessor insert the quotes around the entrypoint name automagically--anybody here know the preprocessor well enough to write a macro or whatever is needed? One could, of course, go back and fix the source file and boot the preprocessor, but that will take a fair amount of work.
I was familiar with the $CDEC pragmas that allowed for how to set the calling mechanisms and decorate names with CVF and is also available in the Intel compiler, but it doesn't appear the gfortran/gcc suite implements the range of options available there; all I've been able to find is that one can set STDCALL, but it has the side effect of appending "@bytes" to the name where "bytes" is the number of bytes on the call stack from the argument list.

1 Commento

The mystery deepens...I went ahead and just built one file to test manually incorporating the surrounding quotes -- but the macro-expanded name isn't actually the entry point name in the library file -- the capitalization is off and the "800" is appended with a prepended underscore -- the objdump output for the symbol in the .lib file is
SYMBOL TABLE:
...
[ 7](sec 1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x0000000000000000 mexErrMsgIdAndTxt_800
[ 8](sec 5)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x0000000000000000 __imp_mexErrMsgIdAndTxt_800
whereas the Include file has the #define as
#define mexErrMsgIdAndTxt mexErrMsgIdAndTxt800
#define mexerrmsgidandtxt mexerrmsgidandtxt800
#define MEXERRMSGIDANDTXT MEXERRMSGIDANDTXT800
There are various capitalizations defined in the .h header file that don't resolve to the same entry point -- I suppose this means that with a supported compiler they built those entry points instead but trying to make C interop work will take making the include file also match it appears.
By fixing up the name, I did get a link step to complete -- I just did it at the command line with a console program so nothing happened, but it did not error with accvio or other exception...
Next step would be to build a minimal actual sample mex file with enough fixups to satisfy the externals needed and then see if that crashes MATLAB... :)
One still would need a way to do the stringizing automagically that is the subject Q? in order to be able to do this in a reasonable fashion--I've yet to find an example of how to do that...the convoluted one I saw in the cpp preprocessor doc didn't work for this purpose for some reason I couldn't figure out.

Accedi per commentare.

Risposte (0)

Prodotti

Release

R2020b

Richiesto:

dpb
il 8 Dic 2021

Commentato:

dpb
il 9 Dic 2021

Community Treasure Hunt

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

Start Hunting!

Translated by