我必须在fortran程序中导入一个MAT文件。我跟踪了示例文件,但是我在链接时遇到了一些问题。编译进行得很好。
最低代码:
#include "fintrf.h"
PROGRAM main
USE ssa
USE dmotifs
USE param
IMPLICIT NONE
! MAT-FILE Declarations !
INTEGER matOpen, matGetDir
INTEGER matGetVariableInfo
INTEGER mp, dir, adir(100), pa
INTEGER mxGetM, mxGetN, matClose
INTEGER ndir, i, clstat
CHARACTER*32 names(100)
!===========================!
if(all(fnames(:)%fn .NE. argfun)) then
write(*,*) "No such motif: ",argfun
write(*,*) "Input format-> main <motifname>"
stop
else
fin=fchton(argfun)
y0=nM2m*analys(p,argfun)
! ==> OPEN MAT-file <== !
mp=matOpen('./PRMS_lxr_29Apr15.mat','r')
if (mp .eq. 0) then
write(6,*) "Can't open MAT-file"
stop
end if
dir = matgetdir(mp, ndir)
if (dir .eq. 0) then
write(6,*) "Can't read MAT-file-directory."
stop
endif
call mxCopyPtrToPtrArray(dir, adir, ndir)
do 20 i=1,ndir
call mxCopyPtrToCharacter(adir(i), names(i), 32)
20 continue
write(6,*) 'Directory of Mat-file:'
do 30 i=1,ndir
write(6,*) names(i)
30 continue
write(6,*) 'Getting Header info from first array.'
pa = matGetVariableInfo(mp, names(1))
write(6,*) 'Retrieved ', names(1)
write(6,*) ' With size ', mxGetM(pa), '-by-', mxGetN(pa)
call mxDestroyArray(pa)
clstat=matClose(mp)
end if
END PROGRAM main我使用默认命令为compiling+linking使用gfortran4.8.3:
gfortran main.f90 dmotifs.o param.o ssa.o -o main当我不包括:#include "finitrf.h"时,这段代码编译得很好(没有链接),否则编译器会说
Warning: main.f90:1: Illegal preprocessor directive我试着将finitrf.h重命名为finitrf.f90,但这并没有什么区别。然而,在链接过程中,我得到了以下错误:
main.f90:(.text+0x3ea): undefined reference to `matopen_'
main.f90:(.text+0x487): undefined reference to `matgetdir_'
main.f90:(.text+0x52b): undefined reference to `mxcopyptrtoptrarray_'
main.f90:(.text+0x583): undefined reference to `mxcopyptrtocharacter_'
main.f90:(.text+0x71b): undefined reference to `matgetvariableinfo_'
main.f90:(.text+0x804): undefined reference to `mxgetm_'
main.f90:(.text+0x855): undefined reference to `mxgetn_'
main.f90:(.text+0x89c): undefined reference to `mxdestroyarray_'
main.f90:(.text+0x8b0): undefined reference to `matclose_'
collect2: error: ld returned 1 exit status我需要一个makefile还是在编译命令中添加额外的参数?
编辑:
我添加了-cpp选项,这消除了Illegal preprocessor directive的问题
现在,当我用路径编译到matlab外部组件(在finitf.h中)时,我仍然会得到相同的错误。
gfortran main.f90 dmotifs.o param.o ssa.o -I/usr/local/matlab2008a/extern/include -L/usr/local/matlab2008a/extern/lib -cpp -o main如果我为/usr/local/matlab2008a/bin/glnxa64提供了包含libmat.so在内的其他matlab库的库路径,我仍然会得到相同的错误。
发布于 2015-05-12 09:40:14
正如Alexander所指出的,编译器需要预处理器使用-cpp选项来识别头文件,而不是将其视为非法文件。
链接需要finitrf.h,它通常位于<matlabroot>/extern/include中,基本库存在于<matlabroot>/bin/<arch>/中。
但是仅仅指定这一点是行不通的,精确的matlab库的规范似乎是必要的;它们是libmat.so和libmx.so。
这些库又依赖于其他库,因此需要另一个标志来设置rpath。
最后,它使用以下命令:
gfortran main.f90 dmotifs.o param.o ssa.o -I/usr/local/matlab2008a/extern/include -L/usr/local/matlab2008a/bin/glnxa64 -cpp -o main -lmat -lmx -Wl,-rpath /usr/local/matlab2008a/bin/glnxa64/或一般情况
gfortran program.f90 -I<matlabroot>/extern/include -L<matlabroot>/bin/<arch> -cpp -lmat -lmx -Wl, -rpath <matlabroot>/bin/<arch> -o program.out还请参阅这 post,它与C中的相同问题有关。
发布于 2015-05-12 08:45:32
对于小写文件扩展名*.f90或*.f,预处理器通常被停用。为此,可以将(主)文件重命名为大写扩展名*.F90或*.F,或者提供相应的命令行选项(-cpp for gfortran,-fpp for ifort)。
假设缺少的子程序/函数实际上是在fintrf.h中声明的,这将解决您的问题。
另外,您应该告诉编译器链接到包含Matlab函数的库中。
https://stackoverflow.com/questions/30185571
复制相似问题