Fortran parameter type not included in the compiled object

I have a Fortran module that contains some variables that have a parameter attribute, and some have a save attribute. parameter not included in the compiled object, which becomes a problem when trying to build a library. For example, consider the file testModule.f90 :

 module testMOD integer, save :: thisIsSaved = 1 integer, parameter :: thisIsParametered = 2 end module testMOD 

I will compile this with: ifort -c testModule.f90 . When I check what's inside it:

 >$ nm testModule.o 0000000000000000 T testmod._ 0000000000000000 D testmod_mp_thisissaved_ 

there is only thisIsSaved variable. I know that I can just change thisIsParametered to save , and not to parameter , but ideally I would like the communication user to not change this value. Is there any way to do this?

Edit: I would like this library to be available for C codes, not just Fortran.

+4
source share
2 answers

It really should be saved in a .mod file. All data types and function prototypes are stored there, so you need to enable it when you send someone a .lib file. Try connecting in the module after using it in something else, and it should work fine.

Essentially, a .mod file performs the same task as a .h file in c, so of course you have to include it in your library.

[Update:] If you are trying to use this in C, then, as you said, there is no way to simplify saving a named constant. Alternatively, you can use the protected attribute for the object. At least with Fortran, everything outside the module is limited to writing to a variable. I don't know if the C compiler and linker will respect this behavior, but I think this is probably your best shot.

 module testMOD INTEGER, PROTECTED, BIND(C) :: globalvar = 1 end module testMOD 

Unfortunately, I don't understand much about interoperability with C, so I cannot guarantee that C will respect the protected attribute and will not allow the variable to be changed.

+4
source

As others noted, a parameter is a named constant, and implementations may not allocate memory for this constant in object code (especially for scalars).

Your library should provide a header file for your C clients. You can define the value of the Fortran parameter in this header file, either using #define or const.

This requires maintaining the parameter value in two places, but you already have this maintenance burden with other aspects of the library interface.

+4
source

All Articles