fortran link error

2019-09-07 06:26发布

问题:

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