0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried to convert the program into module but I got another 'undefined reference to' error that was associated with libgfortranbegin.a.
That is a completely different problem. You can ask a new question. Show your code and the complete error message.
Please remove the edit, ask a new question and show the full code there. See minimal reproducible example. The code must also include the calling code.
Could you please elaborate further about how to declare the subroutine external after the main program. I tried by writing external ram_cpl after the end program statement, but it is giving an error.
I solved this problem by changing the program into module. I created another main program which calls this subroutine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.