# Prototype for calling Fortran function with a vector and two integers from C program

**URL:** <https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275>\
**Category:** Uncategorized\
**Created:** [April 19, 2022, 9:29pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275 "2022-04-19T21:29:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giraffe](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@giraffe](https://fortran-lang.discourse.group/u/giraffe)\
**Post date:** [April 19, 2022, 9:29pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/1 "2022-04-19T21:29:50Z")

</div>

I have a C program that calls a Fortran function. I’m getting the below compiler error message. Can you give me examples on how to fix this with a C prototype?

```auto
error: implicit declaration of function ‘test_’ [-Werror=implicit-function-declaration]
`if (test_(&x, y, &z) != 0);`

```

```auto
      INTEGER FUNCTION TEST(A,B,C)
      REAL B(*)
      INTEGER A,B
      
      RETURN 
      END

```

```auto
int main ()
{
  int x;
  float y;
  int z;
  
  if (test_(&x, y, &z) != 0)
    printf ("Yes \n");
}

```

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [April 19, 2022, 9:56pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/2 "2022-04-19T21:56:34Z")

</div>

> [@giraffe](#):
>
> Can you give me examples on how to fix this with a C prototype?

```txt
$ gfortran -c test.f
$ gcc main.c test.o -o main
$ ./main
Yes 

```

```fortran
C test.f
C
      INTEGER(C_INT) FUNCTION TEST(A,B,C) BIND(C)
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_FLOAT, C_INT
      REAL(C_FLOAT) B(*)
      INTEGER(C_INT) A, C ! note the error in your Fortran routine
      TEST = 42_C_INT
      RETURN 
      END

```

```cpp
// main.c

#include <stdio.h>

int test(int *a, float *b, int *c); // missing declaration in your main program
                                    // no need for trailing underscore '_' due to
                                    // BIND(C) attribute on the Fortran side
int main()
{
  int x;
  float y;
  int z;
  
  if (test(&x, &y, &z) != 0)
    printf ("Yes \n");
}

```

Note the usage of `BIND(C)` as the standard way of overcoming compiler name mangling issues. You also won’t have to carry the trailing `_` around anymore.

Putting a bit more effort into your minimal working example would go a long way.

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [April 20, 2022, 3:24pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/4 "2022-04-20T15:24:53Z")

</div>

> [@giraffe](#):
>
> I do believe that people can be prejudiced to what they perceive to be unnecessary clutter if they only want the code to work on a specific operating system and aren’t planning for other contingencies.

In principle a compiler may choose to change the name mangling scheme between versions and render your code useless even on the same OS/compiler. Also if you put your procedure(s) in a module then the module name will probably be included in the name mangling and renaming the module would again break your code.

iso\_c\_binding is there to _reduce_ clutter and errors, not the opposite.

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [April 20, 2022, 3:48pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/6 "2022-04-20T15:48:09Z")

</div>

The underscore at the end of your function call in main.c is the result of the name mangling scheme of your fortran compiler. [Name mangling - Wikipedia](https://en.m.wikipedia.org/wiki/Name_mangling)

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [April 20, 2022, 3:53pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/7 "2022-04-20T15:53:39Z")

</div>

It’s not documented, but [this](https://gcc.gnu.org/legacy-ml/fortran/2012-10/txt00000.txt) _might_ be the way gfortran currently does name mangling:

```auto
Current GFortran name mangling
------------------------------

Currently the name mangling is a bit ad-hoc, with several different
prefixes depending on which part of the compiler is used:

- Procedure "foo" in module "bar": __bar_MOD_foo

- Runtime library: _gfortran_XXX

- Runtime library internal functions (not visible if symbol visibility
  is supported): _gfortrani_XXX

- ISO_C_BINDING functions in the library: __iso_c_binding_XXX

- OOP stuff: __class_XXX

```

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [April 20, 2022, 4:40pm UTC](https://fortran-lang.discourse.group/t/prototype-for-calling-fortran-function-with-a-vector-and-two-integers-from-c-program/3275/8 "2022-04-20T16:40:30Z")

</div>

> [@giraffe](#):
>
> If I were to take my code containing all this “ISO\_C\_BINDING” stuff and give it to some people they’d twist it from my hands and then look for ways to get rid of me.

My, entirely personal, advice would be to “run, do not walk” from such a workplace.

The “ISO\_C\_BINDING stuff” is how Fortran talks to C and vice versa. Everything else is technical debt.
