module linking and program compilation (novice question)

8 views
Skip to first unread message

Civil Eng

unread,
Jun 1, 2009, 5:40:20 AM6/1/09
to gg95
hi all,

I'm studying a program which is compiled with G95.In this program each
subroutine/function resides in seperate file (such as getname.f95)
orginal creator has created a mod file (such as main_int.f95) to
compile and run the programs it just calls "Main" in their USE clause
where each subroutine/function is known to compiler at the compile
time.

content of "getname.f95"
SUBROUTINE getname(argv,nlen)
.....
....! Implementations goes here
...
END SUBROUTINE getname

content of main_int.f95
MODULE main
INTERFACE
SUBROUTINE getname(argv,nlen)
.....
..... Implementations for all subroutines goes here
.....
END INTERFACE
END MODULE main

When I compile the main_int.f95 the program creates "main.mod" and
"main_int.o" file and when calling for execution with the following
command
"g95 -o new_program.exe C:\source\library\main\main_int.f95 E:
\g95\bin\Projects\P44.f95"

compiler show errors for each called subroutines/functions
"undefined reference to '_getname_'
.....
......
.....

Source code and the program to be executed are in different partition
of hard disk could that be reason of compilation errors ?

Any help will be appreciated

Regards,

Doug

unread,
Jun 1, 2009, 7:53:22 AM6/1/09
to gg95
If you have another main program, not shown here, you can compile the
components separately.

"g95 -c C:\source\library\main\main_int.f95"
"g95 -c E:\g95\bin\Projects\P44.f95"

If all goes well you will get two object files that you can then link
in a main program. Something like this:

"g95 -o new_program.exe main_int.o P44.o" main.f95"

Doug

Civil Eng

unread,
Jun 1, 2009, 8:42:10 AM6/1/09
to gg95
hi Doug,

Let's assume that I've two files just "main_int.f95" (module file
where each subroutine/functions is bundled together in that file) and
main program "P44.f95" which uses main.mod in its USE clause to
retrieve necessary subroutine.

content of P44.f95
PROGRAM p44
USE main
IMPLICIT NONE
INTEGER,PARAMETER::iwp=SELECTED_REAL_KIND(15)
...........
...........
END PROGRAM p44


After compiling these files, I got all required object files
"main.mod", "main_int.o" , "p44.o" under the g95\bin directory

So which command I've to use to link them ?

with command
g95 -o new_program.exe main_int.o P44.o "main.f95" I
got the same error mesages

with command
g95 -o new_program.exe main_int.o P44.o "Projects\P44.f95" The
result is same

P.S. I even noticed that this time compiler raises the error mesages
for "P44".o as well
P44.o: P44.f95 : <.text+0x50c> undefined reference to '_getname_'
.....
.....


Thanks in advance

Civil Eng

unread,
Jun 1, 2009, 12:47:08 PM6/1/09
to gg95
Any help please!!

Civil Eng

unread,
Jun 1, 2009, 1:19:10 PM6/1/09
to gg95
Aggghh Sorry my bad,

Main_int.f95 comprises only from subroutine/function declarations with
variables there are no implementation in that source file so it's kind
of header file. The real implementation of subroutines/functions
resides in seperate file where filename is associated with subroutine
(probably for sake of clarity and flexibility)
The content is "main_int.f95

MODULE main
!
INTERFACE
!
SUBROUTINE bandred(a,d,e)
IMPLICIT NONE
INTEGER,PARAMETER::iwp=SELECTED_REAL_KIND(15)
REAL(iwp),INTENT(IN OUT)::a(:,:)
REAL(iwp),INTENT(OUT)::d(0:),e(0:)
END SUBROUTINE bandred
....
....
SUBROUTINE getname(argv,nlen)
IMPLICIT NONE
INTEGER::narg
INTEGER,INTENT(OUT)::nlen
CHARACTER(*),INTENT(OUT)::argv
INTEGER::lnblnk,iargc
END SUBROUTINE getname
....
....
END INTERFACE
!
END MODULE main

So how am I supposed to compile that file, which has dependency to
each subroutine, and provide input for the main program ?


On 1 Haziran, 19:47, Civil Eng <tanjuoz...@gmail.com> wrote:
> Any help please!!

Doug

unread,
Jun 1, 2009, 3:46:19 PM6/1/09
to gg95
In your first post you said "each subroutine/function resides in
seperate file (such as getname.f95)" so you will have to compile each
of these subroutines, or list them all when compiling main_int.f95.

If you have all these files in C:\source\library\main, you should
probably compile your module in that directory:

cd C:\source\library\main
g95 -c main_int.f95 getname.f95 ... <add the names of all the other
files containing subroutines contained in main_int.f95>


Hope this helps,

Doug

Civil Eng

unread,
Jun 1, 2009, 5:01:29 PM6/1/09
to gg95
Thanks for your help Doug

> In your first post you said "each subroutine/function resides in
> seperate file (such as getname.f95)" so you will have to compile each
> of these subroutines, or list them all when compiling main_int.f95.
>
Yes they are. Compiled them with g95 -c .....\main\*.f95

> If you have all these files in C:\source\library\main, you should
> probably compile your module in that directory:

I've compiled the module in that directory as well but result same. It
seems that trying to compile with that module, compiler never
recognize the inlcuded subroutines.

On the contrary compiling with following command everything works
fine, never refering to mod file just dealing with source files
g95 c:\source\library\main\*.f95 P44.f95 -o MyP44.exe

After googling around for a while I found that "main_int.f95" is
interfaced module it contains only subroutine headers that is
subroutine's name, argument list and declaration of argument types.
AFAIU the purpose of keeping the each subroutine in seperate file is,
to addopt the alterations made to the program more conveniently. Any
changes that made to those seperate files can easily be updated in
main_int.f95 (I presume that this is benefits of smart-compiling)

The thing that I couldn't figured out is, how to get that worked with
mod file ?

It may seem that I'm dancing around the same issue even I've overcome
it, but I'm sure that one way or the other I'll face in the future so
I prefer to face it now.





Reply all
Reply to author
Forward
0 new messages