问题
I've been trying lately to use libraries in Fotran, but I kept on getting this error message
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status
which I cound't find any specific solution to it. In this case, I was working with some libraries that I built myself (this problem happened with static and shared libraries) from simples modules I wrote for purposses of tests.
I decided to try only the modules then, and I kept getting the same error message regardes of the module I used. I would like to know if someone can help me on telling if I am using incorrect syntax. Here is the module
module modulo1
IMPLICIT NONE
real, parameter:: pi=3.1415
end module modulo1
This is the main
program teste
use modulo1
IMPLICIT NONE
real :: r = 2
write (*,*) 'Área: ', pi*r**2
end program teste
These were the commands I used for compiling
gfortran -c modulo1.f90
gfortran -c teste.f90
gfortran -o teste.o modulo1.o
回答1:
Your compilation is broken. The command
gfortran -o teste.o modulo1.o
tells gfortran
to create an executable called teste.o
from the object file called modulo1.o
. Since that module file doesn't contain a program
the compiler can't find an entry point for the executable it is trying to build. The argument to the -o
option is the name of the executable to build.
You probably ought to replace that statement with something like
gfortran -o test teste.o modulo1.o
which will build an executable called test
.
In the longer run learn how to use make
or another build system.
来源:https://stackoverflow.com/questions/47081298/cant-compile-fortran-modules-undefined-symbol-main