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

fortran call c: link can't found c function

cean
New Contributor II
502 Views

Hi,

I am trying to call c from fortran following this: Chapter 11 C-Fortran Interface (Fortran Programming Guide) (oracle.com)

Simple Data Types

but can't find c function when link.  The C function is csim_, but the linker wants _csim. What I have done wrong?

fmain.f

 

      integer i
      real r
      external CSim
      i = 100
      call CSim(i,r)
      end

 

csim.c

 

void csim_(int *i, float *r)
{
    *r = *i;
}

 

error:

 

D:\intel\FcallC>icx /c csim.c
Intel(R) oneAPI DPC++/C++ Compiler for applications running on IA-32, Version 2021.2.0 Build 20210317
Copyright (C) 1985-2021 Intel Corporation. All rights reserved.


D:\intel\FcallC>ifort fmain.f csim.obj /link /out:a.exe
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.16.27040.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:fmain.exe
-subsystem:console
/out:a.exe
fmain.obj
csim.obj
fmain.obj : error LNK2019: unresolved external symbol _CSIM referenced in function _MAIN__
a.exe : fatal error LNK1120: 1 unresolved externals

 

Cheers,

Cean

 

0 Kudos
2 Replies
mecej4
Honored Contributor III
491 Views

The documentation contains an entire chapter on mixed language programming. Fortran 2003+ provides a portable (i.e., compiler independent) facility for using C and Fortran together.

You used the GNU convention of naming the C external symbol in lower-case, with an underscore appended: "csim_". For Intel Fortran, use "CSIM", instead.

cean
New Contributor II
486 Views

I was wondering why it is looking for all capital letters. After also removing the underscore, I got an exe file.

Thank you very much.

 

0 Kudos
Reply