How to mix fortran and C ++ in visual studio 2010?

I am trying to call a C ++ function from a main Fortran program. for this I followed the below steps in Visual Studio 2010: To create a C ++ static library project

  • On the menu bar, select File, New, Project.
  • In the left pane of the New Project dialog box, expand Installed, Templates, Visual C ++, and then select Win32.
  • In the center pane, select Win32 Console Application.
  • Provide a name for the project β€” for example, MathFuncsLib β€” in the Name field. Provide a name for the solution, such as StaticLibrary, in the Permission Name field. Select the "OK" button.
  • On the Overview page of the Win32 Application Wizard dialog box, click Next.
  • On the Application Settings page, under Application Type, select Static Library.
  • On the Application Options page, under Advanced Options, clear the Precompiled Header check box.
  • Select the Finish button to create the project.

To create an executable Fortran project

  • On the menu bar, select File, New, Project.
  • In the left pane of the New Project dialog box, expand Installed, Templates, Intel (R) Visual Fortran, and then select Console Application.
  • In the center area, select Empty Project.
  • Provide a name for the project, and then provide a name for the solution. In the solution window, select Add to Solution. Select the "OK" button.

Also, I am doing some customization in Visual Studio as shown below:

  • Right-click the Fortran executable project and select Dependencies to set the executable project depending on the static library project.
  • Right-click the executable project and select Set as Startup Project so you can build and debug it. I have the following main Fortran program and C ++ function.

Fortran program

    program main

      use iso_c_binding, only : C_CHAR, C_NULL_CHAR

      implicit none

      interface
        subroutine print_C ( string ) bind ( C, name = "print_C" )
          use iso_c_binding, only : C_CHAR
          character ( kind = C_CHAR ) :: string ( * )
        end subroutine print_C
      end interface

      call print_C ( C_CHAR_"Hello World!" // C_NULL_CHAR )

    end

      

C ++ function

# include <stdlib.h>
# include <stdio.h>

extern "C" void print_C (char *text)
{
  printf("%s\n", text);
}

      

When I create a program, I will run into the following errors:

Error 1: error LNK2019: unresolved external symbol _print_C referenced in function _MAIN__  Fortranmain.obj 
Error 2: fatal error LNK1120: 1 unresolved externals    Debug\Fortranmain.exe   

      

Can anyone help me? Any suggestion would be much appreciated.

+3


source to share


1 answer


You need to link the C ++ library with the Fortran executable. Dependencies dictate the build order.



  • Right click on the Fortran executable project and select Properties
  • Add directory containing C ++ library assembly for configuration properties -> Linker - General -> Additional library directories (this step may not be needed)
  • Add c ++ library .lib to configuration properties -> Linker -> Input -> Additional Dependancies
+2


source







All Articles