I've found out a strange result while linking a program with C++ and Fortran. The test (see after) is very simple : define a variable in a fortran module and access it in the C++ program. If I compile the fortran (gfortran from GCC 7.4.0) with the option "-finit-local-zero" then the link fails complaining about a multiple definition of the variables. Of course, if I suppress this option, it works well but it's a pb for me cause I need to set to "0" my uninitialized data.
I put here after the fortran code, the C++ code and the commands for making the exe.
module A_module
USE ISO_C_BINDING
implicit none
real(C_FLOAT) , bind(C) :: isareal
integer(C_INT), bind(C, NAME="MMS") :: MMS
CONTAINS
subroutine CHANGE() bind(c)
use iso_c_binding
implicit none
isareal = 5.0
MMS=40
write(*,*) 'fortran changes isareal =>',isareal
write(*,*) 'fortran changes MMS =>',MMS
end subroutine CHANGE
end module
#include <iostream>
using namespace std;
//Fortran definitions "translated" to C++
extern "C" {
void change();
float isareal;
int MMS; // IMPORTANT TO WRITE "mms" and NOT "MMS"
}
int main() {
// Can access to a variable in a fortran module ?
cout << "In C before: isareal = " << isareal;
cout << " and MMS = " << MMS << "\n";
change(); //call fortran to change the values
// Control
cout << "In C after: isareal = " << isareal;
cout << " and MMS = " << MMS << "\n";
return 0;
}
And the commands:
mpif90 -finit-local-zero -c mod_A_module.f
mpicxx -Wextra -std=c++11 -c cpp_main_1.cpp
mpif90 *.o -std=c++11 -lstdc++ -o bad.exe
./bad.exe
here is the error (when using -finit-local-zero)
mod_A_module.o:(.bss+0x0): multiple definition of `isareal'
cpp_main_1.o:(.bss+0x0): first defined here
mod_A_module.o:(.bss+0x4): multiple definition of `MMS'
cpp_main_1.o:(.bss+0x4): first defined here
Thank you in advance for your help...
Aucun commentaire:
Enregistrer un commentaire