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

how to mixed fortran and C++ in visual studio 2010 ?

bluecolorsea_SH
Beginner
667 Views

Hi,

I tried to call the c++ function from fortran main code.

finally, I found the sample MLP code from  ...\Intel\Composer XE 2011 SP1\Samples\en_US\Fortran   and i tried it.

but, I don't know how to set the Project setting for calling c++ function from fortran main program

So, I need the help for detailed procedure for mixing fortran and c++ in visual studio 2010 ,please

This is my fortran and c++ code(In the same sample project,.. my code working well but,not another project)

and I used same sample source code but, doesn't work in another new project.

---- Fmain.f90--------------------------------------------------

PROGRAM fmain2
IMPLICIT NONE

INTERFACE

SUBROUTINE ADD (a,b,RES) BIND(C,NAME='c_add')
USE,INTRINSIC :: ISO_C_BINDING ! Declares C kinds
INTEGER(c_int),INTENT(IN) :: a
INTEGER(c_int),INTENT(IN) :: b
INTEGER(c_int) :: RES
END SUBROUTINE ADD

SUBROUTINE SUB (a,b,RES) BIND(C,NAME='c_sub')
USE,INTRINSIC :: ISO_C_BINDING ! Declares C kinds
INTEGER(c_int),INTENT(IN) :: a
INTEGER(c_int),INTENT(IN) :: b
INTEGER(c_int) :: RES
END SUBROUTINE SUB

END INTERFACE

INTEGER :: a =2
INTEGER :: b =4
INTEGER :: RES

CALL ADD (a,b,RES)
WRITE(*,101) a,b,RES
101 FORMAT (1X,'IN FORTRAN a=',I3,' ,b= ',I3,' ,a+b= ', I4)

CALL SUB (a,b,RES)
WRITE(*,102) a,b,RES
102 FORMAT (1X,'IN FORTRAN a=',I3,' ,b= ',I3,' ,a-b= ', I4)

END PROGRAM

--- Csub.cpp ------------------------------------------------------------

#include <iostream>
using namespace std;

extern "C"
{
void c_add(int &a,int &b, int &RES)
{
RES = a+b;
cout << "In c_routine a ="<< a << " b =" << b << " a+b = " << RES << endl;
}

void c_sub(int &a,int &b, int &RES)
{
RES = a-b;
cout << "In c_routine a ="<< a << " b =" << b << " a-b = " << RES << endl;
}

}

----------------------------

0 Kudos
5 Replies
Steven_L_Intel1
Employee
667 Views
The C++ code has to go into a C++ static library project. (Visual C++ Project > Win32 > Win32 Project. Click Next and then select Application > Static Library. The C++ project then needs to be made a dependent of the Fortran project (right click on the Fortran project, select Dependencies.). Also, change the Fortran > Libraries > Runtime Library option from Debug Multithreaded to Debug Multithread DLL to match the C++ project.
0 Kudos
bluecolorsea_SH
Beginner
667 Views
Hi, and thank you very much for the swift reply! This help me very much. Now, my codes work well, but it printed out warning message like this ------------------------------------------------------- 2>------ Rebuild All started: Project: For, Configuration: Debug Win32 ------ 2>Deleting intermediate files and output files for project 'For', configuration 'Debug|Win32'. 2>Compiling with Intel(R) Visual Fortran Compiler XE 12.1.2.278 [IA-32]... 2>for.f90 2>Linking... 2>win32_project.lib(Cpp.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification 2>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library 2>Embedding manifest... 2> ------------------------------------------------------- Could you tell me how to solve this problem, and what's mean this message?
0 Kudos
Steven_L_Intel1
Employee
667 Views
The 4075 warning you can ignore. The 4098 warning is because you did not follow the step of making the runtime library types consistent between the projects. C++ defaults to DLL libraries, Fortran to static libraries. You have to change one or the other so that they match. (In C++, this is done on the Code Generation property page.)
0 Kudos
farzin_p_
Beginner
667 Views

Dear Steve Lionel ,

It was my problem too!

thanks a lot! your clearly a FORTRAN VIRTUOSO!!!

0 Kudos
Steven_L_Intel1
Employee
667 Views

By the way, in a version coming out this fall we are changing the library defaults when creating new VS projects to match C++, which will avoid this class of problem, though people will need to adjust...

0 Kudos
Reply