- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, every one. I am a little confused about the USE Statement in fortran, assume this:
module mod1 integer :: a = 1 end module mod1 module mod2 use mod1 integer :: b = 2 end module mod2 program test use mod1 use mod2 write(*,*) a, b end program test
Since I have reference mod1 from mod2 through USE Statement, When I reference mod1 again in program section, there's no error occur, don't they conflict or multiple defining? Thanks for reply.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is clear in Test you only need to USE mod2 but there is no conflict as there is no multiple definition because the "a" from mod2 is exactly the same "a" that is in mod1, "a" is only actually defined once.
If it was not this way it would be a nightmare in large complex projects. Where you do have conflicts extensive use of the ONLY clause or the rename facility of USE is very helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Got it Andrew, Thanks times a million.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's interesting that you ask this as I recently had a discussion with the editor of the standard regarding this very topic While the behavior Andrew describes is what we want to happen, the standard lacks sufficient words to specify it and in at least one place has comments that are contradictory. I have a paper in progress for the next standards meeting to discuss an edit to make the behavior clear.
The idea is that nested USE references the "ultimate" declaration and it's not a redeclaration of the entity. This is complicated by the standard allowing you to tack on VOLATILE or ASYNCHRONOUS to a USE-associated variable (something I wish had not been added.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh, so we are directing to the standard issues :-) . I think the way Andrew mentioned may be useful to try at present. Wishing some improvement to be made by the meeting. Thanks a lot, Steve.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It would be easy to explain it the way Andrew did if it weren't for the pesky feature of being able to add attributes to a use-associated variable. But for the most part his explanation is fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Indeed, adding attributes to use-associated variables are pesky things anyway. I find out a way of using such as ONLY, PRIVATE and PUBLIC to strict access to the module, that should be fine and seems work.
module test use, intrinsic :: iso_fortran_env, only: int32 implicit none private public :: a integer(int32) :: a = 5 end module test program main use test implicit none write(*,*) a ! No. 1 write(*,*) int32 ! No. 2 end program main
So line No. 2 will generate an error message of variable int32 is not defined yet.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page