fortran link error

▼魔方 西西 提交于 2019-12-13 03:36:53

问题


I have a problem with compiling a fortran program with the gfortan complier. The main program is located in main.f. So, I write in console:

gfortran D:\test\test.f

But it displays me a lot of errors such as:

 C:\Users\Efds~1\AppData\Local\Temp\cchFNGgc.o:test.f:<.test+0x3a>: undefined reference to '_gridw_'
 C:\Users\Efds~1\AppData\Local\Temp\cchFNGgc.o:test.f:<.test+0x3a>: undefined reference to '_gridz_'
 etc.

I think it's because of functions gridw, gridz etc. are located in other *.f files. But I don't know how to link these all together.

Also, I tried to use Compaq Visual Fortran Complier, but it didn't help me.


回答1:


A basic command for compiling and linking multiple source files into one executable would be

gfortran -o executable source1.f source2.f source3.f

taking care that any .f file you specify is named to the right of any other source files on which it depends. All of this, and much more besides, is well covered in the compiler's documentation.




回答2:


As noted above, you can compile several files with the same command, but it's quite unusual.

You may prefer first compile to object files (".o") :

gfortran -c gridw.f
gfortran -c gridz.f

And then compile the program

gfortran test.f grodw.o gridz.o

If you have many files to link, it may be interesting to build a library:

ar cru mylib.a gridw.o gridz.o
gfortran test.f mylib.a

If you name your library libSOMETHING.a, you can simply write

gfortran test.f -lSOMETHING


来源:https://stackoverflow.com/questions/12641684/fortran-link-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!