LNK2001 and LNK2019 unresolved external symbols -- Link errors in a Visual C++ / Intel Visual Fortran Solution

LNK2001 and LNK2019 unresolved external symbols -- Link errors in a Visual C++ / Intel Visual Fortran Solution

My Visual studio 2010 solution file for a typical purpose consists of one fortran project(of static library type and consists of source1.f90), one C/C++ project(of application type and contains main.cpp) and 13 C/C++ project(of static library type and contains different .cpp/.h files for different classes).
My purpose is to call the some of the functions in fortran source files from one of the C/C++ static library type project.But i am not being able to build the program and getting errors which reads like.
a) My first trial was to call the fortran subroutine from main.cpp. But i am getting the error reads like below,

Error    2    error LNK2019: unresolved external symbol "void __cdecl bar_ftn(int,char *)" (?bar_ftn@@YAXHPAD@Z) referenced in function _main    G:\VS2010\uakron\sourcefiles\application\main.obj
Error    3    error LNK1120: 1 unresolved externals    G:\VS2010\uakron\build\win\debug\application_app.exe    1

     source1.f90 reads as;
     subroutine bar_ftn ( len_input_file, input_file ) bind( c )
     use, intrinsic :: iso_c_binding, only : c_int
     implicit none
     integer(c_int), value, intent(in) :: len_input_file
     character(len=1), intent(in) :: input_file(len_input_file)

    ! Local declarations (copy c char array into fortran character)
    character(len=len_input_file) :: infile
    integer :: i

    print *, "in bar_ftn"
    print *, len_input_file
    do i=1,len_input_file
    end do
    end subroutine bar_ftn

main.cpp reads as;
#include<iostream>
#include<fstream>

   using namespace std;
   extern void bar_ftn ( int flag_len, char* flag );
  static void
  DisplayUsage(char* programName);

    int main(int argc, char *argv[])
      {
       char ctext[]="helloworld abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
        int ctext_len=sizeof(ctext);

      //Call the Fortran
      bar_ftn( ctext_len, ctext );

    return 0;
}
b) On the other side i also called the fortran function from one of the class functions from one of the C/C++ static library project but i am getting the same type of error (LNK2019)

Any help would be highly appreciated.

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

You need to surround the prototype of the subroutine with extern "C"[..}. That is,

extern "C" {void bar_ftn ( int flag_len, char* flag );}

@mecej4 :Thank you so much...I really appreciate your help..now it worked...:)

Leave a Comment

Please sign in to add a comment. Not a member? Join today