Reputation: 55
I am working on a numerical project with C++, but it will use several fortran subroutines in another fortran project. The fortran project has head files and multiple subroutine files. The file dependency is shown in the figure below:
I am wondering that how can I implement this interface in visual studio? Can I set up two projects, one is for fortran, it includes all fortran code, another is for Cpp, in the header file and class file I use extern "C" key word to declare the fortran subroutine?
Update1: The compiler for fotran is Intel Fortran, the compiler for C++ is msvc using MFC and the standard windows libraries.
Upvotes: 1
Views: 1291
Reputation: 7267
Create an executable project (and VS Solution) for the C++ part. Now add to that solution a Fortran Static Library project and add your Fortran files to it.
Right click on the C++ project and select Build Dependencies > Project Dependencies. Check the box of the Fortran library project to make it a dependent of the C++ project. In older versions of VS, this is all you would need to have the library linked in, but MS removed that function for non-Microsoft compilers, so you have some additional steps:
This should be all you need to build. You will likely want to use the C interoperability features in your Fortran code, including adding BIND(C) to any routines called by the C++ code, with a NAME= specifier with the case-sensitive name C++ will use. Be aware that the Fortran VALUE attribute means pass-by-value only when the routine has BIND(C).
There is a worked C_Calls_Fortran example in the Intel Fortran samples bundle at https://software.intel.com/content/www/us/en/develop/download/776976.html (under compiler_f\MixedLanguage). If you need more help, ask in the Intel Fortran user forum at https://community.intel.com/t5/Intel-Fortran-Compiler/bd-p/fortran-compiler
Upvotes: 3