Can't use allocate command in Fortran Mex file

5 ビュー (過去 30 日間)
yuehua Feng
yuehua Feng 2017 年 10 月 31 日
コメント済み: yuehua Feng 2017 年 11 月 1 日
If I delete allocate, this file can be compiled successfully, but when I added the allocate, it will be an error. My matlab version is R2016b, and fortran version is GFORTRAN (GCC 6.3.0) and Xcode 7.3. The details are following. What should I do if I need to keep allocate?
#include<fintrf.h>
subroutine MEXFUNCTION(nlhs, plhs, nrhs, prhs)
implicit none
MWPOINTER :: plhs(*), prhs(*)
MWPOINTER :: mxCreateDoubleMatrix, mxGetPr
MWPOINTER :: something
integer :: nrhs, nlhs, nargin, nargout
mwSignedIndex :: num
real*8 mxGetScalar
! plhs(1) = mxCreateDoubleMatrix(zero,zero,mxREAL)
!Check for proper number of arguments
nargin = 1
nargout = 1
if(nrhs .ne. nargin) then
call mexErrMsgTxt('One input required.')
elseif(nlhs .gt. nargout) then
call mexErrMsgTxt('Too many outputs.')
end if
num = int(mxGetScalar(prhs(1)))
plhs(1) = mxCreateDoubleMatrix(num, num, 0)
something = mxGetPr(plhs(1))
! Call computational subroutine:
call make_matrix(num, %val(something))
return
end
subroutine make_matrix(num, something)
implicit none
integer :: num, i, j
real(kind = 8), allocatable :: something2(:, :)
real(kind = 8) :: something(num, num)
allocate(something2(num, num))
do i = 1, num
do j = 1, num
something2(i, j) = i * j
end do
end do
CALL DLACPY('ALL', num, num, something2, num, something, num)
deallocate(something2)
return
end
The error is :
Undefined symbols for architecture x86_64:
"__gfortran_os_error", referenced from:
_make_matrix_ in make_matrix.o
"__gfortran_runtime_error", referenced from:
_make_matrix_ in make_matrix.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
(use -v to see invocation)

採用された回答

James Tursa
James Tursa 2017 年 10 月 31 日
編集済み: James Tursa 2017 年 10 月 31 日
First thing you should do is clean up your argument typing to be consistent. You have the following:
mwSignedIndex :: num
:
call make_matrix(num, %val(something))
:
subroutine make_matrix(num, something)
integer :: num, i, j
It is entirely possible that the "mwSignedIndex" type is a 64-bit integer while the default "integer" type is a 32-bit integer ... even on a 64-bit system. Since the make_matrix subroutine is using an implicit interface, the compiler/linker will not catch this potential error. You should always ensure you have the same argument types, so maybe change that declaration in the subroutine to:
mwSignedIndex :: num, i, j
You may need to change that call to DLACPY as well.
Next thing you should do is NEVER use literal constants in Fortran for the arguments to the MATLAB API functions. You can get away with this in C/C++ where arguments are passed by value and you get automatic type promotion from the compiler ... but you can't get away with this in Fortran where you might have a mismatch since arguments are effectively passed by reference. Again, one typical cause is the 64-bit integer vs 32-bit integer thing. So always use the exact type specified in the API signature. In this case, the signature for mxCreateDoubleMatrix is this:
mwPointer mxCreateDoubleMatrix(m, n, ComplexFlag)
mwSize m, n
integer*4 ComplexFlag
So you should change your code to use these exact variable types, and always pass a variable and not a literal constant. So change this:
mwSignedIndex :: num
:
num = int(mxGetScalar(prhs(1)))
plhs(1) = mxCreateDoubleMatrix(num, num, 0)
to something like this:
mwSignedIndex :: num
mwSize m, n
integer*4 ComplexFlag
:
num = int(mxGetScalar(prhs(1)))
m = num
n = num
ComplexFlag = 0
plhs(1) = mxCreateDoubleMatrix(m, n, ComplexFlag)
After making these changes to ensure it is not an argument type mismatch issue, then we can work on the allocate/deallocate issue. You should probably put in a memory allocation failure check in your code for the allocate statement, btw.
  2 件のコメント
yuehua Feng
yuehua Feng 2017 年 10 月 31 日
Thanks. I modified my code per your suggestions, but it still has error. DLACPY is a Lapack routine, I wonder whether I need to modify this routine. Since if I modify this routine, I also need to modify all other Lapack routines.
#include<fintrf.h>
subroutine MEXFUNCTION(nlhs, plhs, nrhs, prhs)
implicit none
MWPOINTER :: plhs(*), prhs(*)
MWPOINTER :: mxCreateDoubleMatrix, mxGetPr
MWPOINTER :: something
integer :: nrhs, nlhs, nargin, nargout
mwSignedIndex :: num
mwSize m, n
integer*4 ComplexFlag
real*8 mxGetScalar
!Check for proper number of arguments
nargin = 1
nargout = 1
if(nrhs .ne. nargin) then
call mexErrMsgTxt('One input required.')
elseif(nlhs .gt. nargout) then
call mexErrMsgTxt('Too many outputs.')
end if
num = int(mxGetScalar(prhs(1)))
m = num
n = num
ComplexFlag = 0
plhs(1) = mxCreateDoubleMatrix(m, n, ComplexFlag)
something = mxGetPr(plhs(1))
! Call computational subroutine:
call make_matrix(num, %val(something))
return
end
subroutine make_matrix(num, something)
implicit none
mwSignedIndex :: num, i, j
double precision, allocatable :: something2(:, :)
double precision :: something(num, num)
integer :: ierr
allocate(something2(num, num),stat=ierr)
if(ierr /= 0) then
call mexPrintf('Memory error!')
end if
do i = 1, num
do j = 1, num
something2(i, j) = i * j
end do
end do
CALL DLACPY('ALL', num, num, something2, num, something, num)
deallocate(something2)
return
end
The error is:
>> mex -largeArrayDims -v make_matrix.F90 -lmwlapack -lmwblas
Verbose mode is on.
... Looking for compiler 'gfortran' ...
... Looking for environment variable 'DEVELOPER_DIR' ...No.
... Executing command 'which gfortran' ...No.
... Looking for file '/usr/local/bin/gfortran' ...Yes.
... Looking for folder '/usr/local/gfortran/bin' ...Yes.
... Executing command 'which gfortran' ...No.
... Looking for file '/usr/local/bin/gfortran' ...Yes.
... Executing command '/usr/local/bin/gfortran -print-file-name=libgfortran.dylib' ...Yes ('/usr/local/gfortran/lib/libgfortran.dylib').
... Looking for folder '/' ...Yes.
... Executing command 'which gfortran' ...No.
... Looking for file '/usr/local/bin/gfortran' ...Yes.
... Executing command '/usr/local/bin/gfortran -print-file-name=libgfortranbegin.a' ...Yes ('libgfortranbegin.a').
... Looking for folder '/' ...Yes.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk' ...Yes.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk' ...Yes.
... Executing command 'echo /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk | rev | cut -c1-10 | rev | egrep -oh '[0-9]+\.[0-9]+'' ...Yes ('10.11').
Found installed compiler 'gfortran'.
Options file details
-------------------------------------------------------------------
Compiler location: /usr/local/gfortran/bin
Options file: /Users/yuehuafeng/Library/Application Support/MathWorks/MATLAB/R2016b/mex_FORTRAN_maci64.xml
CMDLINE2 : xcrun -sdk macosx10.11 clang -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.11 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/fexport.map" -O /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o -lmwlapack -lmwblas -L"/Applications/MATLAB_R2016b.app/bin/maci64" -lmx -lmex -lmat -L"/" -lgfortran -lgcc_s.1 -o make_matrix.mexmaci64
FC : /usr/local/gfortran/bin/gfortran
FDEFINES :
FFLAGS : -fexceptions -m64 -fbackslash -cpp
INCLUDE : -I"/Applications/MATLAB_R2016b.app/extern/include" -I"/Applications/MATLAB_R2016b.app/simulink/include"
FOPTIMFLAGS : -O
FDEBUGFLAGS : -g
LDF : xcrun -sdk macosx10.11 clang
LDFLAGS : -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.11 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/fexport.map"
LDBUNDLE : -bundle
LINKEXPORT : -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/fexport.map"
LINKLIBS : -lmwlapack -lmwblas -L"/Applications/MATLAB_R2016b.app/bin/maci64" -lmx -lmex -lmat -L"/" -lgfortran -lgcc_s.1
LDOPTIMFLAGS : -O
LDDEBUGFLAGS : -g
OBJEXT : .o
LDEXT : .mexmaci64
DEVELOPER_DIR_CHECK :
GFORTRAN_INSTALLDIR : /usr/local/gfortran/bin
GFORTRAN_LIBDIR : /
GFORTRANBEGIN_LIBDIR : /
ISYSROOT : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
SDKVER : 10.11
MATLABROOT : /Applications/MATLAB_R2016b.app
ARCH : maci64
SRC : /Users/yuehuafeng/Documents/MATLAB/test/make_matrix.F90;/Applications/MATLAB_R2016b.app/extern/version/fortran_mexapi_version.F
OBJ : /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o;/var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o
OBJS : /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o
SRCROOT : /Users/yuehuafeng/Documents/MATLAB/test/make_matrix
DEF : /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.def
EXP : make_matrix.exp
LIB : make_matrix.lib
EXE : make_matrix.mexmaci64
ILK : make_matrix.ilk
MANIFEST : make_matrix.mexmaci64.manifest
TEMPNAME : make_matrix
EXEDIR :
EXENAME : make_matrix
OPTIM : -O
LINKOPTIM : -O
CMDLINE1_0 : /usr/local/gfortran/bin/gfortran -c -I"/Applications/MATLAB_R2016b.app/extern/include" -I"/Applications/MATLAB_R2016b.app/simulink/include" -fexceptions -m64 -fbackslash -cpp -O /Users/yuehuafeng/Documents/MATLAB/test/make_matrix.F90 -o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o
CMDLINE1_1 : /usr/local/gfortran/bin/gfortran -c -I"/Applications/MATLAB_R2016b.app/extern/include" -I"/Applications/MATLAB_R2016b.app/simulink/include" -fexceptions -m64 -fbackslash -cpp -O /Applications/MATLAB_R2016b.app/extern/version/fortran_mexapi_version.F -o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o
CMDLINE3_0 : rm -f /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o
CMDLINE3_1 : rm -f /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o
-------------------------------------------------------------------
Building with 'gfortran'.
/usr/local/gfortran/bin/gfortran -c -I"/Applications/MATLAB_R2016b.app/extern/include" -I"/Applications/MATLAB_R2016b.app/simulink/include" -fexceptions -m64 -fbackslash -cpp -O /Users/yuehuafeng/Documents/MATLAB/test/make_matrix.F90 -o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o
/Users/yuehuafeng/Documents/MATLAB/test/make_matrix.F90:27:38:
call make_matrix(num, %val(something))
1
Warning: Type mismatch in argument 鈥榮omething鈥 at (1); passed INTEGER(8) to REAL(8)
/usr/local/gfortran/bin/gfortran -c -I"/Applications/MATLAB_R2016b.app/extern/include" -I"/Applications/MATLAB_R2016b.app/simulink/include" -fexceptions -m64 -fbackslash -cpp -O /Applications/MATLAB_R2016b.app/extern/version/fortran_mexapi_version.F -o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o
xcrun -sdk macosx10.11 clang -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.11 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/fexport.map" -O /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o /var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o -lmwlapack -lmwblas -L"/Applications/MATLAB_R2016b.app/bin/maci64" -lmx -lmex -lmat -L"/" -lgfortran -lgcc_s.1 -o make_matrix.mexmaci64
Error using mex
ld: warning: object file
(/var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/make_matrix.o)
was built for newer OSX version (10.12) than being linked (10.11)
ld: warning: object file
(/var/folders/yv/_khzm5qs2xvf65r74s0f66j80000gn/T/mex_4582563152808_35078/fortran_mexapi_version.o)
was built for newer OSX version (10.12) than being linked (10.11)
ld: warning: ignoring file /usr/local/lib/libgfortran.dylib, file was
built for i386 which is not the architecture being linked (x86_64):
/usr/local/lib/libgfortran.dylib
Undefined symbols for architecture x86_64:
"__gfortran_runtime_error_at", referenced from:
_make_matrix_ in make_matrix.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
yuehua Feng
yuehua Feng 2017 年 11 月 1 日
I have solved this problem, as I made a mistake in gfotran.xml. Thanks for your advice.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by