Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran calling C library

emmavartdal
Beginner
428 Views

I'm having some problems making a Fortran application link with my C libraries. I've had plenty of luck doing it with the gcc C and Fortran compiler, but Intel Fortran(our preferred Fortran compiler) is being a bit difficult.

I'm developing my C library with VS2008, and using Intel Fortan 10 on VS 2005. My C files look like this:

== AC_LIB_STATIC.H ==
#ifndef _AC_LIB_STATIC_H_
#define _AC_LIB_STATIC_H_

extern "C" int ADD(int*, int*, int*);

#endif // _AC_LIB_STATIC_H_

== AC_LIB_STATIC.C ==

int ADD(int* a, int* b, int* answer) {
    int ans;
    ans = (*a+*b);
    *answer = ans;

    *answer = (*a+*b);

    return(1);
}

The ADD function gets declared as "int __stdcall ADD(int*, int*, int*) " and decorated as "?ADD@@YGHPAH00@Z"

== TEST.F90 ==

    program Console1

    implicit none

    ! Variables
    INTEGER :: A = 1
    INTEGER :: B = 3
    INTEGER :: AN = 0
    
    ! Body of Console1
    print *, 'Hello World'
    
    CALL ADD(A, B, AN)
    PRINT *, AN
 
    end program Console1

And when running this I end up with(After placing ac_lib_static.lib in the library path):

Error  1   error LNK2019: unresolved external symbol _ADD referenced in function _MAIN__  Console1.obj  

So, what am I doing wrong here?

0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
428 Views
Quoting - emmavartdal

I'm having some problems making a Fortran application link with my C libraries. I've had plenty of luck doing it with the gcc C and Fortran compiler, but Intel Fortran(our preferred Fortran compiler) is being a bit difficult.

I'm developing my C library with VS2008, and using Intel Fortan 10 on VS 2005. My C files look like this:

== AC_LIB_STATIC.H ==
#ifndef _AC_LIB_STATIC_H_
#define _AC_LIB_STATIC_H_

extern "C" int ADD(int*, int*, int*);

#endif // _AC_LIB_STATIC_H_

== AC_LIB_STATIC.C ==

int ADD(int* a, int* b, int* answer) {
int ans;
ans = (*a+*b);
*answer = ans;

*answer = (*a+*b);

return(1);
}

The ADD function gets declared as "int __stdcall ADD(int*, int*, int*) " and decorated as "?ADD@@YGHPAH00@Z"

== TEST.F90 ==

    program Console1

implicit none

! Variables
INTEGER :: A = 1
INTEGER :: B = 3
INTEGER :: AN = 0

! Body of Console1
print *, 'Hello World'

CALL ADD(A, B, AN)
PRINT *, AN

end program Console1

And when running this I end up with(After placing ac_lib_static.lib in the library path):

Error  1   error LNK2019: unresolved external symbol _ADD referenced in function _MAIN__  Console1.obj  

So, what am I doing wrong here?


1) Intel Fortran does not use __stdcall anymore (as CVF did), but __cdecl
2) You must #include "ac_lib_static.h" within ac_lib_static.c, so that the compiler sees that extern "C" (as a rule of thumb, every Foobar.c file should #include its own Foobar.h)
0 Kudos
Reply