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

VS-2019/ifort 2023 C calling Fortran

gib
New Contributor II
1,308 Views

This topic has probably been done to death, but I have been away from the forum for a long time.

For many years I've stuck with Fortran 11 on VS2005, because I could always do everything I needed.  Now I need to move completely to 64-bit (to enable linking of a Fortran-built DLL to a 64-bit C++ program.)  I have installed VS2019 Community, and the latest Intel oneAPI HPC Toolkit.  I am building a test C++ program with the MS compiler, and building the DLL with Intel Fortran 2023.1 (both inside VS2019, both x64). 

My call to a DLL subroutine is now failing.

The Fortran looks like this:

subroutine set_greens(Ngreen) bind(C)
!DEC$ ATTRIBUTES DLLEXPORT :: set_greens
use, intrinsic :: iso_c_binding
integer(c_int), value :: Ngreen

if (Ngreen == 0) then
greens = .false.
else
greens = .true.
NgreenCells = Ngreen
endif

end subroutine

 

The C++ has:

extern "C" void set_greens(int);

int main()

{

    int Ngreen;

    Ngreen = 7500;

    set_greens(Ngreen);

}

I have told the C++ linker where to find the .lib for the DLL, but the link error is:

"unresolved external symbol _set_greens"

I have tried changing bind(C) to bind(C, name='set_greens') but the error persists.  This used to work in the 32-bit world.  What am I now doing wrong?

Thanks.

0 Kudos
1 Solution
gib
New Contributor II
1,290 Views

I apologise.

I didn't notice that the target of the test program was still set to W32.  Changing that to x64 enabled the compilation.  Running the test failed because some Fortran DLLs were not found.  Adding their location to the PATH solved that, and now the test runs.

View solution in original post

2 Replies
gib
New Contributor II
1,291 Views

I apologise.

I didn't notice that the target of the test program was still set to W32.  Changing that to x64 enabled the compilation.  Running the test failed because some Fortran DLLs were not found.  Adding their location to the PATH solved that, and now the test runs.

MWind2
New Contributor III
1,289 Views

Assuming you are using a c++  project named "cppf" and a fortran dll project named "dll1" in a Solution, first right-click on project cppf "Build Dependencies" and choose projects-> dll1. Then go to cppf project properties and "Linker->General->Additional Library Directories and prepend "..\dllf1" resulting in "..\dll1\$(IntDir);%(AdditionalLibraryDirectories)", and then go to "Linker->Input and prepend "dll1.lib" so it looks like "dll1.lib;%(AdditionalDependencies)". Finally in cppf project Build Events->Pre-Build Event "copy $(SolutionDir)dll1\$(IntDir)dll1.dll $(SolutionDir)$(IntDir)".

0 Kudos
Reply