链接导入MAT文件的Fortran代码时出错 [英] Errors in linking fortran code that imports a MAT-file
问题描述
我必须在fortran程序中导入MAT文件。我遵循示例文件,但我遇到了一些问题同时链接。
最小代码:
#include fintrf.h
程序main
USE ssa
USE dmotifs
USE参数
隐式无
! MAT-FILE声明!
INTEGER matOpen,matGetDir
INTEGER matGetVariableInfo
INTEGER mp,dir,adir(100),pa
INTEGER mxGetM,mxGetN,matClose
INTEGER ndir,我,clstat
字符* 32个名字(100)
!=========================== !
if(all(fnames(:)%fn .NE。argfun))then
write(*,*)No such motif:,argfun
write(* ,*)输入格式 - >主< motifname>
stop
else
fin = fchton(argfun)
y0 = nM2m * analys(p,argfun)
! ==>打开MAT文件< ==!
mp = matOpen('./ PRMS_lxr_29Apr15.mat','r')
if(mp .eq。0)then
write(6,* )无法打开MAT文件
stop
end if
dir = matgetdir(mp,ndir)
if(dir .eq 。0)then
write(6,*)无法读取MAT文件目录。
stop
endif
call mxCopyPtrToPtrArray(dir,adir,ndir)
do 20 i = 1,ndir
call mxCopyPtrToCharacter(adir (i),姓名(i),32)
20继续
写(6,*)'文件目录:'
30 i = 1,ndir
write(6,*)names(i)
30 continue
write(6,*)'从第一个数组获取标题信息'$ b $ (6,*)'已检索',名称(1)
write(6,*)'With size',mxGetM(pa), '-by-',mxGetN(pa)
调用mxDestroyArray(pa)
clstat = matClose(mp)
如果
结束END PROGRAM main
我使用gfortran 4.8.3编译+使用默认命令链接:
gfortran main.f90 dmotifs.o param.o ssa.o -o main
当我不包含以下代码时,该代码可以很好地编译(无链接): #includefinitrf.h
,否则编译器会说:
警告:main.f90:1:非法预处理器指令
我尝试将 finitrf.h
重命名为 finitrf。 f90
但它没有任何区别。然而,在链接期间,我收到了这些错误:
main.f90 :(。text + 0x3ea):未定义引用'matopen_'
main.f90 :( .text + 0x487):对`matgetdir_'的未定义引用
main.f90 :( .text + 0x52b):对`mxcopyptrtoptrarray_'的未定义引用
main.f90: (.text + 0x583):对'mxcopyptrtocharacter_'的未定义引用
main.f90 :( .text + 0x71b):未定义对`matgetvariableinfo_'的引用
main.f90 :( .text + 0x804):undefined引用'mxgetm_'
main.f90 :(。text + 0x855):对`mxgetn_'的未定义引用
main.f90 :( .text + 0x89c):对`mxdestroyarray_'的未定义引用
main.f90 :( .text + 0x8b0):对`matclose_'的未定义引用
collect2:错误:ld返回1退出状态
我是否需要makefile或在编译命令中添加其他参数?
编辑:
我添加了 -cpp
选项并消除了 Ill例如,预处理程序指令
现在,当我用matlab外部组件(其中finitf.h)编译路径时,错误。
gfortran main.f90 dmotifs.o param.o ssa.o -I / usr / local / matlab2008a / extern /包括-L / usr / local / matlab2008a / extern / lib -cpp -o main
如果我提供库路径为 / usr / local / matlab2008a / bin / glnxa64
,其中包含其他matlab库,包括 libmat.so
,I仍然会有同样的错误。正如Alexander Vogt指出的那样,编译器需要 -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
另见这个帖子与 C
中的问题大致相同。
I have to import a MAT-file in a fortran program. I followed the example file but I am facing some problems while linking. The compilation happens fine.
Minimal code:
#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
I am using gfortran 4.8.3 for compiling+linking using the default command:
gfortran main.f90 dmotifs.o param.o ssa.o -o main
This code compiles fine (without linking) when I do not include: #include "finitrf.h"
, otherwise the compiler says
Warning: main.f90:1: Illegal preprocessor directive
I tried renaming finitrf.h
to finitrf.f90
but it did not make any difference. Nonetheless during linking I am getting these errors:
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
Do I need a makefile or add additional arguments in the compile command?
EDIT:
I added the -cpp
option and that eliminates the problem of Illegal preprocessor directive
Now when I am compiling with paths to matlab external components (where finitf.h) is, I am still getting the same error.
gfortran main.f90 dmotifs.o param.o ssa.o -I/usr/local/matlab2008a/extern/include -L/usr/local/matlab2008a/extern/lib -cpp -o main
If I provide library path to /usr/local/matlab2008a/bin/glnxa64
that contains other matlab libraries including libmat.so
, I still get the same errors.
As pointed out by Alexander Vogt, the compiler requires -cpp
option for the pre-processor to recognize the header file and not to treat it as illegal.
Linking requires finitrf.h
which is usually located in the <matlabroot>/extern/include
and the essential libraries are present in <matlabroot>/bin/<arch>/
.
But just specifying this does not work and specification of the exact matlab library seems essential; these are libmat.so
and libmx.so
.
These libraries are in turn dependent on other libraries so another flag is required to set the rpath
.
Finally it works with following command:
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/
or in general
gfortran program.f90 -I<matlabroot>/extern/include -L<matlabroot>/bin/<arch> -cpp -lmat -lmx -Wl, -rpath <matlabroot>/bin/<arch> -o program.out
Also see this post that is about the same problem in C
.
这篇关于链接导入MAT文件的Fortran代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!