fortran-iso-c-binding

Mixed C++ and Fortran Linking Issue

送分小仙女□ 提交于 2019-12-07 06:53:52
问题 I have done some searching online but I cannot find out how to compile a simple C++ and Fortran code from linux. I need to get complex with it, but I just need to know how to start with a simple example. My C++ code is this: #include <iostream> using namespace std; extern int Add( int *, int * ); extern int Multiply( int *, int * ); int main() { int a,b,c; cout << "Enter 2 values: "; cin >> a >> b; c = Add(&a,&b); cout << a << " + " << b << " = " << c << endl; c = Multiply(&a,&b); cout << a <

sockets programming gfortran

会有一股神秘感。 提交于 2019-12-07 01:22:55
问题 I want to be able to call networking functions in my Fortran application. My boss wants me to do everything in Fortran instead of using C and Fortran. We have already done a version of the application using PGI's Fortran compiler on Windows. We are moving it to Linux where we will probably use their compiler. Right now, I'm using gfortran. I have created an interface for these networking calls, and everything compiles and links. The code below is something similar to what I'm doing except the

How to prepare a fortran module from a C header file?

末鹿安然 提交于 2019-12-06 13:16:13
I have this pure C file with 6 functions that I want to make available for fortran programmers: http://tinyfiledialogs.sourceforge.net Could C to Fortran "Import" work ? Should I simply prepare something like a Fortran header file instead ? I realise that my C code is using several Unix or Windows C header files, and this will complicate the conversion. But surely offering the equivalent of a Fortran header for 6 C functions must be achievable. char const * tinyfd_inputBox ( char const * const aTitle , char const * const aMessage , char const * const aDefaultInput ) ; // the returned value is

Why aren't my fortran functions exported when using the BIND(C, NAME=“name”) attribute

走远了吗. 提交于 2019-12-06 12:46:11
I am used to using the following syntax subroutine CalcA(A,N) !DEC$ ATTRIBUTES DLLEXPORT :: CALCA !DEC$ ATTRIBUTES ALIAS:'CalcA' :: CalcA IMPLICIT NONE ... end subroutine CalcA which produces an exported function in a .dll So now I am trying the new ISO_C_BINDING with the following code subroutine CalcA(A,N) BIND(C, NAME="CalcA") USE, INTRINSIC :: ISO_C_BINDING IMPLICIT NONE ... end subroutine CalcA But the export function is not created So what am I missing here? How is the new iso_c_binding going to replace the deprecated !DEC$ ATTRIBUTE DLLEXPORT declarations? PS. I am on Intel Fortran XE

Passing pointer from C to fortran Subroutine

不羁岁月 提交于 2019-12-06 10:43:24
I am trying to call a fortran subroutine from C, can I allocate in C and pass the pointer to Fortran safely? The array in the subroutine is an automatic array (x(nmax)). (I am allocating the x and then passing it to the fortran) Yes. Modern Fortran guarantees that Fortran routines can be called from C and vice-a-versa. This is done via the Fortran ISO_C_BINDING. This is part of Fortran 2003 and was widely available as an extension to Fortran 95 compilers. There is documentation in the gfortran manual (Chapters "Mixed-Language Programming" and "Intrinsic Modules".) As a language feature, this

c-fortran interoperability - derived types with pointers

依然范特西╮ 提交于 2019-12-06 07:57:08
I have long fortran code that has to be made usable from python. I decided to do Fortran->C->Python interface. I got a problem: I have derived types within Fortran modules that contain double precision, allocatable type as members. When trying to compile with ifort I get (with gfortran something similar): Each component of a derived type with the BIND attribute shall be a nonpointer, nonallocatable data component with interoperable type and type parameters This is actually with agreement with Intel compiler documentation and Fortran 2003 standard: point 15.2.5. Is there any way to access

Passing a dynamic 2D array from C++ to Fortran and back

五迷三道 提交于 2019-12-06 05:24:06
Passing a fixed 2D array between C++ and Fortran works fine, however not so with the program I have written to pass a 2D dynamic array from C++ to Fortran. C++ side extern "C" {void array2d_(double **, int *, int *); } using namespace std; int main() { double **array; int nx=3; int ny=2; int i,j; cout << "Passing dynamic array from C to Fortran\n"; array = (double **) malloc(nx * sizeof(double *)); if(array == NULL) { fprintf(stderr, "out of memory\n"); exit; } for(i = 0; i < nx; i++) { array[i] = (double *) malloc(ny * sizeof(double)); if(array[i] == NULL) { fprintf(stderr, "out of memory\n")

Use Fortran-code in C

天涯浪子 提交于 2019-12-06 04:23:14
I try to use a fortran-routine in C, but I doesn't work. I don't know where I made a mistake. Here my Fortran-code including the Integration-Module, which I want to use in C: module integration implicit none contains function Integrate(func, a,b, intsteps) result(integral) interface real function func(x) real, intent(in) :: x end function func end interface real :: integral, a, b integer :: intsteps intent(in) :: a, b, intsteps optional :: intsteps real :: x, dx integer :: i,n integer, parameter :: rk = kind(x) n = 1000 if (present(intsteps)) n = intsteps dx = (b-a)/n integral = 0.0_rk do i =

What is the official way to deal with string in C++/FORTRAN interop

流过昼夜 提交于 2019-12-05 14:35:56
I'd like to learn the latest improvement in C++/FORTRAN interoperability when it comes to string in particular. The following is my unsuccessful attempt, please help me correct or advise a better solution. My compiler is gcc 4.8.5 In C++ #include <iostream> extern "C"{ void SayHello(char*); } int main(int argc, char** argv){ char * name = argv[1]; SayHello(name); return 0; } In Fortran module MyModule contains subroutine SayHello(people) bind(c,name="SayHello") use, intrinsic :: iso_c_binding character, dimension(50), intent(in) :: people write(*,*) "Hello ", people end subroutine end module

Mixed C++ and Fortran Linking Issue

久未见 提交于 2019-12-05 11:21:11
I have done some searching online but I cannot find out how to compile a simple C++ and Fortran code from linux. I need to get complex with it, but I just need to know how to start with a simple example. My C++ code is this: #include <iostream> using namespace std; extern int Add( int *, int * ); extern int Multiply( int *, int * ); int main() { int a,b,c; cout << "Enter 2 values: "; cin >> a >> b; c = Add(&a,&b); cout << a << " + " << b << " = " << c << endl; c = Multiply(&a,&b); cout << a << " * " << b << " = " << c << endl; return 0; } My Fortran Code is this: integer function Add(a,b)