Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

compiler errors - Undefined reference to 'main' when compiling a module

I am learning Fortran, and I'm stuck trying to compile a module for later use.

In the main program I have this, which asks for two numbers and then calls the function in the module:

use exponentiate
integer::a,b
write *, 'A'
read *, 'a
write *, 'B'
read *, 'b
write *,expo(a,b)

(I haven't tried that out because I need to compile the module first, but that's not the issue)

Then, on another file I have this code, which (If I understood anything correctly) is just a standard module with a function that exponentiates two numbers.

module exponentiate
interface test 
  module procedure expo
end interface
contains
  function expo(a,b)
    type(integer), intent(in)::a,b
    type(integer) expo
    integer::temp
    temp=a
    do i=1,b
      temp=temp*a
    end do
    expo=temp
  end function expo
end module exponentiate

I've been trying to figure the syntax out based on compiler errors since the Fortran 95 specification is unreadable and nearly useless. With that and some Wikipedia/SO help I've been able to figure some things out, but I have no idea why this compiler error pops up.

I'm not sure if this is because of some syntax issue or a misuse of gfortran, so any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Without options to the contrary, that compiler driver (and many others, regardless of language) will assume that they are being given all the components necessary for a complete program or similar - compiling source to object code (compilation proper), linking that object code with any other specified object code or libraries and generating an executable.

In the context of a Fortran program, that process cannot be taken through to completion without a main program of some form. The internal name that many Fortran compilers have for the main program is main or similar (variations exist with case and additional underscores) - you are simply seeing the consequences of your main program not being made available to the compiler driver.

With that compiler driver (and most others), to compile Fortran source through to object code only (i.e. file.f90 -> file.o) supply the -c command line option. You can then provide the resulting file with the object code to a later invocation of the compiler driver when you are ready to build the final executable for your program.

Alternatively, supply the name of the source file with your main program (and the name of any other source files) after the name of the source file for the module on the command line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...