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

Mixed Fortran/ C++ programming problem.

farzin_p_
Beginner
784 Views

Dear all,

I have some problems in compiling the following prototype. It gets LNK2019 and LNK1120 errors. the following code seems to be pretty corrcet. I don't know which IFort settings need to be changed.

program fprogram
      real a,b
      a=1.0
      b=2.0

      print*,'Before Fortran function is called:'
      print*,'a=',a
      print*,'b=',b
      call ffunction(a,b)
      print*,'After Fortran function is called:'
      print*,'a=',a
      print*,'b=',b

      print*,'Before C function is called:'
      print*,'a=',a
      print*,'b=',b
      call cfunction(a,b)
      print*,'After C function is called:'
      print*,'a=',a
      print*,'b=',b

      print*,'Before C++ function is called:'
      print*,'a=',a
      print*,'b=',b
      call cppfunction(a,b)
      print*,'After C++ function is called:'
      print*,'a=',a
      print*,'b=',b

      stop
      end

subroutine ffunction(a,b)
      a=3.0
      b=4.0
      return
      end

extern "C" {
      void cppfunction_(float *a, float *b);
    }

    void cppfunction_(float *a, float *b) {
      *a=5.0;
      *b=6.0;
    }

void cfunction_(float *a, float *b) {
      *a=7.0;
      *b=8.0;
    }

Thanks.

0 Kudos
13 Replies
mecej4
Honored Contributor III
784 Views

Whether your scheme works or not depends on the compiler/OS combination. If you want a robust solution, use the C interoperability features of Fortran 2003.

If you are doing this on Windows,  compile the Fortran part and dump the external symbols from the .OBJ file so produced, and make the function names match in the C code. For IFort, you could make the C function name uppercase with no extra underscore.

0 Kudos
IanH
Honored Contributor II
784 Views

You are making assumption about an aspect of the calling convention (decoration of the name with a trailing underscore) that is not necessarily correct for ifort on Windows.  It is far, far, far more robust for you, as a programmer, to explicitly specify the calling convention by using the widely supported C interoperability features of Fortran 2003.

[fortran]program fprogram
  IMPLICIT NONE
  INTERFACE
    SUBROUTINE cppfunction(a, b) BIND(C, NAME='cppfunction')
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_FLOAT
      IMPLICIT NONE
      ! No value attribute, so these correspond to a pointer formal
      ! parameter in C.
      REAL(C_FLOAT), INTENT(OUT) :: a
      REAL(C_FLOAT), INTENT(OUT) :: b
    END SUBROUTINE cfunction
  END INTERFACE
 
  ! We'll assume here that the default kind for REAL variables for the
  ! Fortran compiler is the same as the C_FLOAT kind from ISO_C_BINDING
  ! (i.e. that 'REAL :: a' is the same as 'float a' in C).  If they are
  ! not the same, then you should get an error message from the compiler
  ! when you try and call cppfunction.
  real a,b
 
  a=1.0
  b=2.0

  print*,'Before C++ function is called:'
  print*,'a=',a
  print*,'b=',b
  call cppfunction(a,b)
  print*,'After C++ function is called:'
  print*,'a=',a
  print*,'b=',b

  stop
end[/fortran]

[cpp]extern "C" void cppfunction(float *a, float *b)
{
   *a=7.0f;
   *b=8.0f;
}[/cpp]

p.s. Use modules for your fortran procedures!

0 Kudos
jimdempseyatthecove
Honored Contributor III
784 Views

>>p.s. Use modules for your fortran procedures!

At least use a module to hold the declaration of your interfaces to your C/C++ program.

Jim Dempsey

0 Kudos
farzin_p_
Beginner
784 Views

Dear all,

Sincerest thanks for all your favourable considerations. I considered your comments in the following attached solution. But still gets LNK2019 and LNK1120 errors.

Thanks again.

0 Kudos
mecej4
Honored Contributor III
784 Views

You declared (in the interface definition) BIND(C,NAME='mean'), but there is no function with the name 'mean' in your file cfunction2.cpp. Secondly, you must surround the function heading in the C++ source with extern "C" {..} to prevent C++ name decoration from occurring. Note that C-interoperability does not imply C++-interoperability.

0 Kudos
farzin_p_
Beginner
784 Views

Dear mecej4,

Sorry for wrong attachment. I followed your instructions. But but problem is still remain. I have attached the corrected files.

Thanks.

0 Kudos
mecej4
Honored Contributor III
784 Views

What problems? I compiled your latest sources using Intel 13.1 in 32-bit mode as well as 64-bit mode, from a command window. In both cases,  the program was built with no messages of any sort, and the program output was correct.

You may need to go through your project settings and files to clean out stale/incorrect pieces of information.

0 Kudos
farzin_p_
Beginner
784 Views

Dear mecej4,

sorry I'm really new  in mixed programming. But when compiling, using  "Intel Parallel Studio XE 2013" It gets following errors:

Error    1    error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup    C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Mixed_FC\Cfunction\MSVCRTD.lib(crtexe.obj)    Cfunction
Error    2    error LNK1120: 1 unresolved externals    C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Mixed_FC\Debug\Cfunction.exe    1    1    Cfunction
Error    3     error LNK2019: unresolved external symbol _mean referenced in function _MAIN__    fmain.obj    
Error    4     fatal error LNK1120: 1 unresolved externals    Debug\Fprogram.exe    

0 Kudos
mecej4
Honored Contributor III
784 Views

Your Fortran project has not been told that it should use the object file from the C project at link time. Secondly, your C project should not be designated as having an EXE target. To fix the latter, click on Cfunction in the Solution Explorer in VS, then ConfigurationProperties->General->ProjectDefaults->ConfigurationType and deselect EXE; this is because the C project cannot stand on its own and is subsidiary to the other project.

You have to specify (in the Solution Explorer window of VS) that the result from the Cfunction "project", namely, the file mean.obj, should be included in the link step. One way of doing this is to click on Fprogram in the Solution Explorer->Add Existing Item->{make your way to the CFunction\Release\ directory, and select mean.obj (assuming that you have built the Cfunction project already). A subsequent build produced the EXE for me.

Since you have a different version of VS and the compilers, I will not attach the modified project file. Furthermore, I almost never use VS to build projects, and someone else may be better equipped to tell you a more proper way of doing the job using VS.

0 Kudos
farzin_p_
Beginner
784 Views

special thanks for your wonderful comments. C project was set to be a Static library and then successfully compiled. I added "Cfunction.lib" in Fortran project. I have successfully used such a manner for using some fortran static libraries. But unfortunately, in this case, it gets the followin errors:

Error    1     error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)    MSVCRTD.lib(ti_inst.obj)    
Error    2     error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)    MSVCRTD.lib(ti_inst.obj)    
Error    4     fatal error LNK1169: one or more multiply defined symbols found    Win32\Fprogram.exe    

just like when we have more than one main program in a project. I don't have any idea any more. Any idea how to resolve this error in Intel Fortran? I have attached the solution.

thanks.

0 Kudos
farzin_p_
Beginner
784 Views

YES! I found the solution in:

http://software.intel.com/en-us/forums/topic/326037

Thanks Steve!

0 Kudos
mecej4
Honored Contributor III
784 Views

The error messages that you show are caused by choosing (actively or by default) different run time libraries for the two projects that make up the solution. I suggest using multithreaded debug DLL libraries for both projects.

The attached file, when unzipped, will create directories and files that will let you build a debug EXE when you select the Mixed_FC project and ask VS to build it. I have limited experience with VS; if the project files in the zip do not work, as may happen if you have different software versions from those I used (VS 2012 Update 2, Intel C and Fortran 2013.3.171) someone else will have to help you.

0 Kudos
farzin_p_
Beginner
784 Views

Dear mecej4,

Specail thanks for your favourable consideration and really helpful comments.

0 Kudos
Reply