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

Issue with VS2022 static Fortran library called on a Cpp project

Edward99
New User
117 Views

We had a disturbing issue within the mentioned above settings. We had defined extern "C" { void my_fortran_sub(); } on the beginning of the cpp file and called the 

my_fortran_sub() from a function into this file.

The my_fortran_sub() exists on the Fortran file .

This file comprises the Fortran static library . 

On summary, as described, we have a project of a cpp file which calls a function which is inside a Fortran static library; on the same VS2022 solution.

We keep getting the following error: "Function definition for 'my_fortran_sub' not found." We tried numerous measures such as adding the bind keyword on the Fortran file, to no avail.

 

Thank you for any help. 

0 Kudos
2 Replies
Ron_Green
Moderator
89 Views

I think this is the same issue an Intel employee relayed to me through an Intel Teams Chat.

 

I had your code.  Here are my notes to the Intel person:

a couple of notes:

I had to switch the C++ Project Properties to build x64 instead of ia32.   

The main problem was that by default, Fortran creates symbols for Subroutines in UPPER CASE.  His C is looking for lower-case "my_fortran_sub".  There is a compiler option and VS Property setting to change this to lower case.  Your original VS2015 project probably was using the option to force lower case names.  But a better way is to force lower case in the name with a NAME clause.  I did this.  In this way you do not rely on a compiler option, which can get missed or changed accidentally (and thus break the linker):

the code:

 

SUBROUTINE my_fortran_sub() BIND(C)

    my change to use NAME clause

SUBROUTINE my_fortran_sub() BIND(C,NAME="my_fortran_sub")

 

Note that this forces the exported name for the subroutine to be in lower case.  Additionally one can change the exported name of this subroutine to any other character string. 

 

That was the main things.

I created a zip of a Solution space and provided that to the Intel employee to forward to you.  Make sure to backup or rename c:\MyProgs folder as I used the same name and path and this zip could overwrite yours.  

 

also found a typo:  he is using the folder for the Fortran and named it "FotranLib" and not "FortranLib".  this results in the library being named "fotranlib.lib" which is confusing.  

 

0 Kudos
Ron_Green
Moderator
89 Views

Also I switched the C++ Project to use the Intel C++ compiler so that all the Fortran runtime libraries would be found easily.

0 Kudos
Reply