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

Pls Help-FORTRAN call C++ function

wisperless
Beginner
681 Views

I have Visual studio 2005 and IVF9.1 compiler installed on 64 bit machine (windows XP for 64 bit OS). I am trying to call a C++ function from FORTAN, but it failed. Here is what I did for the testing.

1). Start a new IVF project (console application) from VS2005 named mixed_programing

Code:

 program mixed_programing
implicit none
INTERFACE
SUBROUTINE TEST(I, J)
!DEC$ ATTRIBUTES REFERENCE :: I,J
INTEGER I,J
END SUBROUTINE
END INTERFACE

integer I,J
CALL TEST(I, J)
print *, 'Hello World'
 end program mixed_programing

2). Add a newwin 32 console projectto this mixed_programing FORTRAN project.

Code:

// C code.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

int _tmain(int argc, TCHAR* argv[])
{
return 0;
}
extern "C" void TEST(int *i,int*j)
{
*i=1;
*j=2;
}

Then compile, it gives me the following error,

1>------ Build started: Project: mixed_programing, Configuration: Debug Win32 ------
1>Compiling with Intel Fortran 9.1 C:Program Files (x86)IntelCompilerFortran9.1IA32...
1>mixed_programing.f90
1>Linking...
1>mixed_programing.obj : error LNK2019: unresolved external symbol _TEST referenced in function _MAIN__
1>Debug/mixed_programing.exe : fatal error LNK1120: 1 unresolved externals

I have been working on solving this issue for couple of days, but still can not make it work. Please help. Thank you!!

Rachel

0 Kudos
5 Replies
Steven_L_Intel1
Employee
681 Views

The C project should be a Static Library project, not a console application, because the latter creates its own executable. Remove the _tmain main program from the C code. Make the C library be a dependent project of the Fortran project (right ckick on the Fortran project and select Dependencies. Make sure that the C and Fortran library settings are compatible (in Fortran this is set under Libraries and in C++ under Code Generation.)

You don't need the !DEC$ ATTRIBUTES line, though it doesn't hurt.

0 Kudos
wisperless
Beginner
681 Views

Steve,

Thank you. It works. :))

A quick question about MS visual studio compiler. Does IVF 9.1 compiler inegrate with any other microsoft visual studio compiler which can run on a 64-bit machine except VS 2005? I am so frustrated with using VS 2005.

Thank you.

Rachel.

0 Kudos
Steven_L_Intel1
Employee
681 Views
No, VS2005 is the only VS which supports development of x64 applications. What are you frustrated about? Have you installed the new Service Pack 1 for VS2005? It fixes some problems that affect Fortran users.
0 Kudos
wisperless
Beginner
681 Views

Steve,

Thank you for your response. I met a lot of problems when I tried to port a project mixed with FORTAN6.1 and VC++6.0 to IVF9.1 and Visual Studio 2005 on a 64-bits machine(OS: windows XP). One of my problems is how to code a project that a C++ will call a function in FORTRAN, and the FORTAN function will call another C++ function?

For example:

int main()

{

int i,j;

C_CALL_FORTRAN(&i.&j);

}

FORTRAN:

subroutine C_CALL_FORTRAN(i,j)

{

integer i,j

i=1

j=1

CALL FORTRAN_CALL_C(i,j)

}

C++ function

void FORTRAN_CALL_C(*i,*j)

Thank you again for your help!

Rachel.

0 Kudos
Steven_L_Intel1
Employee
681 Views

This is no different than any other mixed Fortran-C application. Since the main program is in C, you have a C executable application (console, Win32, whatever) and a Fortran static library project which is made a dependent of the C project. I suggest that you install VS2005 SP1 to fix a bug related to dependent projects.

You also need to take the following steps to get thingsto link properly:

  • In the Fortran project, Properties, Libraries, set "Disable default library search rules" to No.
  • Make sure that the "Use run-time library" setting in the Fortran project is compatible with what is set under "Code Generation" in the C project
  • In Tools..Options..Projects..VC++ Directories, add the Intel Fortran EM64TLIB folder to the list of library folders.
0 Kudos
Reply