Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

Initialize variables inside a module

fjimenem
Beginner
1,027 Views
Hello

I'm building a executable file using the source files provided by a third party and developed for CVF.

Everything seems to go fine, but when I run the exe file a strange thing happens: the variables that the code defines in a module behave as properly defined but NOT initialized, despite the fact that the module gives them a value. The actual project is too big to include here but more or less the problem goes as follows:

module global_tables

implicit none
save
public
...
character*8 :: iname = 'myname'
integer :: inumber = 2
...
end module global_tables


subroutine mytest
USE GLOBAL_TABLES
....
print *, iname
print *, inumber
....
end

When the code arrives to mytest, the output it produces is a blank line. However, if I define iname and inumber as constants (as in character*8, parameter :: iname = 'myname') the output turns out to be correct. I realize that this is probably a trivial problem, as the code is been built and tested in other platforms, but I don't seem to find an easy solution, besides building a function that initializes ALL the variables in the main program. Any suggestion?

Fernando
0 Kudos
6 Replies
fjimenem
Beginner
1,027 Views
Well, actually, the output when I don't declare the variables as contants in a blank line (for the string variable) AND a 0 (for the numerical value) I had forgotten the 0 in the previous post.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,027 Views
The actual small code as above works in 6.6B (as it should). I don't have any idea why it doesn't work for you -- whether it's a programmer's error (but I don't see where it could have been made) or compiler's error due to some unusual stuff or complexity. I suggest you send zip file of your workspace to vf-support@compaq.com.

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,027 Views
I don't think it's the compiler - my guess is that the initialization is happening but that you're overwriting the data with zeroes somewhere in the program. It would be useful to see if the values print correctly at the start of the program.

Steve
0 Kudos
fjimenem
Beginner
1,027 Views
Hi and thanks to everyone who thought about this.
I followed your suggestions and I ended up finding something that may be related to my problem, not a bug of the compiler but a potential source of problems.
If I do the following:
1. compile source_1 (a module with my data)
2. compile source_2 (a source code that uses the previous module)
3. link object_1 and object_2
I get the expected result.
But if then
4. change source_1 adding new data
5. compile source_1
6 link the NEW object_1 and the OLD object_2
The compiler does not find anything suspicious about this, but the data stored in object_1 does not match the data in object_1. The executable is built without warnings, but gives wrong results, precisely when using those variables defined and initialized in the module.

I'm building my code using a makefile given by the institution that developed the code. It *seems* that the order in the compilation is correct, but I have to check and maybe compile file by file, at least for the modules.

Fernando
0 Kudos
Steven_L_Intel1
Employee
1,027 Views
If you recompile a module, you must recompile all sources that USE that module. The IDE will do this automatically, but you need to change your makefile to add dependencies on the various .mod files.

Steve
0 Kudos
fjimenem
Beginner
1,027 Views
Hi to everyone

I found more things about this problem and I think that they are interesting.

The problem, as I see it now, happens the object files of the modules are collected in a library.

If I link directly my main program with the object file everything seems to work. If I build a library with the objects (I am using Microsoft Library Manager version 6) and I link the main program with the library that I get, I arrive to my situation: I have the variable but I don't have the value.

I don't know if this is a problem with the compiler, the library manager or the linker, but if anyone knows an easy solution (a switch or an option when compiling, linking or building the library) I'd be very grateful.

In any case, thanks for your help.
0 Kudos
Reply