Reputation: 147
I have a main program but I want to use it as a subroutine. Therefore, I defined a subroutine inside it
Program main
contains
subroutine ram_cpl
.
.
.
end subroutine ram_cpl
end program main
I am supposed to link it to an executable exe1
and then use further for some external programs. I get exe1
normally but when I call this subroutine in an external program located in a different directory, I get an error
undefined reference to `ram_cpl'
I think the problem is occurring with the linkage as either I should declare this subroutine as public or the linkage should be done properly.
But I can use the PUBLIC
statement only inside a module, I also checked my command line used for linkage but I did not get any clue.
/usr/local/bin/mpif90 -frepack-arrays -O3 -ftree-vectorize -ftree-loop-linear -funroll-loops -w -ffree-form -ffree-line-length-none -frecord-marker=4 -fconvert=big-endian -I.. master.o -o exe2 -L.. ../main.o
where main.o
is the compiled program containing the subroutine and master is the another program that calls it. exe2
is the executable I am trying to get.
This command line runs properly with other programs and I also tried to replace the program main
with module main
but it gave me another error.
Upvotes: 0
Views: 317
Reputation: 59998
Your subroutine is internal to the program where it is declared. You cannot call it from anywhere else.
If you want to call a subroutine from multiple locations, place it in a module, do not make it internal. All subroutines in modern Fortran should be placed in a module unless you have a good reason to place them elsewhere.
You must use
the module when before you call the subroutine from it.
You can also make it external (after end program
), but the module is a better and the modern way to go. External subroutine is like any other, it is just outside of any other program unit. An external
statement should be used in the calling code (often not necessary).
Also, you cannot compile two programs at the same time. Only one main program is allowed.
Upvotes: 3