C Wrapper Calling Fortran Functions

leoleson

I'm trying to write a C wrapper to call a set of functions in a Fortran module. I'm starting with something basic but I'm missing something important.

I've tried appending/prepending different numbers of underscores. I've also tried linking with gcc instead of gfortran. What I show below gives the simplest error(s).

I'm working on a Mac running Yosemite 10.10.3, GNU Fortran 5.1.0, and the C compiler that comes with Xcode.

main.c

#include "stdio.h"

int main(void) 
{
    int a;
    float b;
    char c[30];
    extern int _change_integer(int *a);

    printf("Please input an integer: ");
    scanf("%d", &a);

    printf("You new integer is: %d\n", _change_integer(&a));

    return 0;
}

intrealstring.f90

module intrealstring
use iso_c_binding
implicit none

contains

integer function change_integer(n)
    implicit none

    integer, intent(in) :: n
    integer, parameter :: power = 2

    change_integer = n ** power
end function change_integer

end module intrealstring

Here is how I'm compiling, along with the error:

$ gcc -c main.c
$ gfortran -c intrealstring.f90
$ gfortran main.o intrealstring.o -o cwrapper
Undefined symbols for architecture x86_64:
  "__change_integer", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
$ 
LPs

You have to bind fortran to c:

module intrealstring
use iso_c_binding
implicit none

contains

integer (C_INT) function change_integer(n) bind(c)
    implicit none

    integer (C_INT), intent(in) :: n
    integer (C_INT), parameter :: power = 2

    change_integer = n ** power
end function change_integer

end module intrealstring

Your c file has to modified as follows:

#include "stdio.h"

int change_integer(int *n);

int main(void) 
{
    int a;
    float b;
    char c[30];

    printf("Please input an integer: ");
    scanf("%d", &a);

    printf("You new integer is: %d\n", change_integer(&a));

    return 0;
}

The you can do:

$ gcc -c main.c
$ gfortran -c intrealstring.f90
$ gfortran main.o intrealstring.o -o cwrapper

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fortran: Calling other functions in a function

From Dev

How to write a general wrapper for calling a Fortran function in C++14 (call by reference --> call by value)

From Dev

Calling c++ functions

From Dev

COM wrapper-calling chain C++

From Dev

Difference in Fortran pointer and Fortran allocatable in calling C_F_POINTER

From Dev

Calling a Fortran function from Qt C++

From Dev

Calling C from fortran (ifort, gfortran)

From Dev

Calling C function with **int parameter from Fortran

From Dev

C# calling fortran dll stack overflow

From Dev

Calling a FORTRAN subroutine from C#

From Dev

Calling C function from Fortran with pointers

From Dev

C++ calling DLL functions

From Java

calling assembly functions from c

From Dev

Calling Python functions in C program

From Dev

Calling random functions (C++)

From Dev

C++ template for calling functions

From Dev

Bash wrapper for fortran code

From Dev

Functions in fortran

From Dev

Fortran and C interoperability: receive return value from c (calling from Fortran)

From Dev

Pointer is being being masked when calling a C function from Fortran

From Dev

Calling FORTRAN subroutine from c++ yields illegal parameter value

From Dev

Calling Fortran subroutines with optional arguments from C++

From Dev

segmentation fault when calling fortran subroutine in C++

From Dev

SDL with C++ calling Fortran: Scope and Initializer error

From Dev

Calling Fortran from C# in VS2019 with iFort

From Dev

Error Intel Fortran calling C shared library function that receives an integer

From Dev

Calling plain-C functions with "opaque pointer"

From Dev

Calling Linux Socket and Epoll functions in C#

From Java

Calling C++ functions from Java

Related Related

HotTag

Archive