可以在makefile中使用拉帕克

发布于 2025-02-10 02:51:56 字数 1140 浏览 0 评论 0原文

我有(在Fortran)使用三个自定义模块,这些模块使用Lapack。到目前为止,我使用以下Shell脚本编制了程序:

  filestring="main"
  gfortran -c mod_exp.f90 mod_genmat.f90 mod_print.f90 $filestring.f90
  gfortran mod_exp.o mod_genmat.o mod_print.o $filestring.o -llapack -lblas
  rm mod_exp.o mod_genmat.o mod_print.o $filestring.o exponentiate.mod genmat.mod printing.mod printing_subrtns.mod
  mv a.out $filestring

由于我使用越来越多的模块和使用它们的不同程序,因此我决定开始使用Makefiles。在教程之后,我设法撰写了以下内容:

FC = gfortran
FFLAGS = -Wall -Wextra -llapack -lblas #-fopenmp
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o


%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
  $(FC) $(FFLAGS) -c -O $< -o $@
  
main: $(OBJ)
  $(FC) $(FFLAGS) -o $@ $(OBJ)

clean:
  @rm -f *.o *.mod main

但是,当执行制作时,它说lapack函数无法识别。这样的错误是以下错误:

/usr/bin/ld: mod_exp.o: in function `__exponentiate_MOD_diagun':
mod_exp.f90:(.text+0x37f): undefined reference to `zgees_'
...
collect2: error: ld returned 1 exit status

我看到的一个可能的错误是我需要指定库的位置。但是,由于我以前不需要这样做,这似乎很奇怪。另外,我不知道如何找到它。

I have program (in fortran) where I'm using three custom modules, which make use of LAPACK. Until now I've compiled my program using the following shell script:

  filestring="main"
  gfortran -c mod_exp.f90 mod_genmat.f90 mod_print.f90 $filestring.f90
  gfortran mod_exp.o mod_genmat.o mod_print.o $filestring.o -llapack -lblas
  rm mod_exp.o mod_genmat.o mod_print.o $filestring.o exponentiate.mod genmat.mod printing.mod printing_subrtns.mod
  mv a.out $filestring

Since I've been using more and more modules and different programs using them, I've decided to start using makefiles. Following a tutorial, I managed to write the following:

FC = gfortran
FFLAGS = -Wall -Wextra -llapack -lblas #-fopenmp
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o


%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
  $(FC) $(FFLAGS) -c -O 
lt; -o $@
  
main: $(OBJ)
  $(FC) $(FFLAGS) -o $@ $(OBJ)

clean:
  @rm -f *.o *.mod main

However, when executing make, it says that the LAPACK functions are not recognized. One such mistake is the following:

/usr/bin/ld: mod_exp.o: in function `__exponentiate_MOD_diagun':
mod_exp.f90:(.text+0x37f): undefined reference to `zgees_'
...
collect2: error: ld returned 1 exit status

One possible mistake I've seen is that I need to specify the location of the libraries. However, it would seem strange since I didn't need to do it before; also, I don't know how to find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-02-17 02:51:56

请显示引起调用的链接命令,该命令会生成错误。

我有信心,如果您剪切并粘贴了该确切的命令行,将其与运行时看到的相同错误。因此,问题不是解决的,而是您的链接命令。

问题在于,您已将库放在链接行中的对象之前。库应在对象之后结束时出现,否则,当链接器检查库时,它不知道需要包含哪些符号(因为尚未解析对象以查看丢失的符号)。

这就是为什么ldlibs传统上是一个单独的变量:

FC = gfortran
FFLAGS = -Wall -Wextra #-fopenmp
LDLIBS = -llapack -lblas
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o

%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
        $(FC) $(FFLAGS) -c -O 
lt; -o $@

main: $(OBJ)
        $(FC) $(FFLAGS) -o $@ $(OBJ) $(LDLIBS)

Please show the link command that make invoked, that caused the error to be generated.

I'm confident that if you cut and paste that exact command line to your shell prompt, you will get the same error you see when make runs it. So the problem is not make, but your link command.

The problem is that you have put the libraries before the objects in the link line. Libraries should come at the end, after the objects, else when the linker examines the libraries it doesn't know what symbols will need to be included (because no objects have been parsed yet to see what symbols are missing).

This is why LDLIBS is traditionally a separate variable:

FC = gfortran
FFLAGS = -Wall -Wextra #-fopenmp
LDLIBS = -llapack -lblas
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o

%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
        $(FC) $(FFLAGS) -c -O 
lt; -o $@

main: $(OBJ)
        $(FC) $(FFLAGS) -o $@ $(OBJ) $(LDLIBS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文