Variables declared in F90 module and C routines

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Post Reply
jtache
Posts: 25
Joined: Thu Apr 19, 2012 4:01 pm

Variables declared in F90 module and C routines

Post by jtache »

Dear CS users and CS team,

I searched for an answer to my problem in the forum and CS docs but I didn't find any, so I post it here.

I try to use some F90 variables (defined in mymodule() in cs_user_modules.f90) in a C routine (cs_user_mesh.c).
These variables are intialized at the beggining of the CS run by the use of cs_user_initialization.f90 routine.

I just declared my variable in cs_user_mesh C routine

Code: Select all

extern int myvar
and I try to print it with

Code: Select all

bft_printf("myvar = %6i\n", myvar);

Compiler complains about a "nested extern declaration of myvar" in cs_user_mesh.c (this could be fair as myvar will be known after linking of objects files) and compilation stops with this message :

Code: Select all

cs_user_mesh.c:(.text+0x24) : référence indéfinie vers « myvar»
collect2: error: ld returned 1 exit status
This seems to be a linking problem, so it's probably about a missing declaration of mymodule() or a bad declaration of my variable.

Can you help me and indicate how I can properly include my module in the C routine, just like I "use mymodule" in F90 routines?

Thanks for feebacks!

Jeremie
Yvan Fournier
Posts: 4209
Joined: Mon Feb 20, 2012 3:25 pm

Re: Variables declared in F90 module and C routines

Post by Yvan Fournier »

Hello,

In which module is "myvar" defined ? The full (linkage) name is usually a compound name including that of the module.

To avoid issues, the cleanest solution is to use iso_c_bindings to access the variable (and/or to use a function to access it).

It may be simpler to define the variable in C, and access it using a Fortran pointer (check for example how imrgra and anomax, defined in optcal.f90, and linked in cs_parameters.c using cs_f_space_disc_get_pointers).

Best regards,

Yvan
Post Reply