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

Help with mixed C++/Fortran under .NET framework

scottgrossman
Beginner
1,561 Views
Hi,

I've been running a large fortran program under VC++ 6.0 and Compaq Fortran, and was told to investigate what it would take to port to the latest greatest environment.

Along those lines I wrote a little test program, which I dont seem to be able to get working. I've defined a Solution space with 2 projects, the first being a fortran project that generates a static library, the second being a C++ console application that links in the static library.

The sample code is as follows

C++

#include
#include
#include
#include

extern "C" void FORTRANTEST (int *i);

void main(int argc, _TCHAR* argv[])
{
int i=1;
cout << i << ' ';;

FORTRANTEST (&i);

cout << ' ';
cout << "After calling Fortran routine i= " << i << ' ';;
cout << ' ';

cin >> i;
}


FORTRAN
subroutine FORTRANTEST (int1)
c ! Expose subroutine Fortran to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT:: FORTRANTEST
!DEC$ ATTRIBUTES C, ALIAS:'_FORTRANTEST' :: FORTRANTEST

implicit integer (i-n)

int1 = int1 + 1

return
end

The fortran compiles and generates the library, but when I try to link the C++ solution it says theres an unresolved library libIEPCF90.lib. Alright I dont know why it uses this, but I found it and put it in the additional dependencies list, and relinked, and got an error saying the linker couldn't find Program.obj

Any help would be greatly appreciated. Please feel free to e-mail me at sngsmg@aol.com

Thanks
Scott

0 Kudos
10 Replies
isn-removed200637
1,561 Views
The following slightly modified version works:
// testprog1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 
#include 
#include 

extern "C" void __stdcall FORTRANTEST (int* i);

void main(int argc, _TCHAR* argv[])
{
int i=22;
cout << i << '
';;

FORTRANTEST (&i);

cout << '
';
cout << "After calling Fortran routine i= " << i << '
';;
cout << '
';

cin >> i;
}

SUBROUTINE FORTRANTEST(int1)
interface 
subroutine FORTRANTEST(int1)
!DEC$ ATTRIBUTES C, ALIAS:'_FORTRANTEST@4' :: FORTRANTEST
integer int1
end subroutine FORTRANTEST
end interface
! Expose subroutine Fortran to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT:: FORTRANTEST
integer int1
int1 = int1 + 1
return
end

Note the '@4' added to the alias name. HTH
0 Kudos
isn-removed200637
1,561 Views
P.S. The FORTRAN code was used to generate a library
FORTTEST.LIB that was added to the C++ project.
0 Kudos
scottgrossman
Beginner
1,561 Views
Anthony,

Thanks for the help, but when I tried to compile this:

SUBROUTINE FORTRANTEST(int1)
interface
subroutine FORTRANTEST(int1)
!DEC$ ATTRIBUTES C, ALIAS:'_FORTRANTEST@4' :: FORTRANTEST
integer int1
end subroutine
end interface

! Expose subroutine Fortran to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT:: FORTRANTEST
integer int1
int1 = int1 + 1
return
end

I get the following errors:
C:Testfortran2fortrantest.f(7): Error 275 : Another entity has the same name as procedure FORTRANTEST specified in the interface block ending here
C:Testfortran2fortrantest.f(10): Error 119 : More than one program entity with this name is accessible


Thanks again
Scott
0 Kudos
hweisberg
Beginner
1,561 Views
In the code you pasted in you have

_FORTRANTEST @4'

with an unwanted space before the @4. Could this be the problem?

Major it's just a problem with the software for this forum and you don't actually have an extra space!
0 Kudos
scottgrossman
Beginner
1,561 Views
hweisberg,

It appears to be a bug in the forum posting software. Source as written doesnt have the extra space in it.

Thanks
Scott
0 Kudos
isn-removed200637
1,561 Views
I hope that it is just the forum software screwing up what I hoped
I had pasted correctly. I notice that in your code,the line beginning
the 'INTERFACE' block has been telescoped onto the line
preceding it. I think 'INTERFACE' should start on a new line.
Try that and I hope it will work for you.
Just to repeat, this is what I programmed
in the FORTRAN STATIC library project:

SUBROUTINE FORTRANTEST(int1)

interface 

subroutine FORTRANTEST(int1)
 
!DEC$ ATTRIBUTES C, ALIAS:'_FORTRANTEST@4' :: FORTRANTEST 

integer int1

end subroutine FORTRANTEST

end interface

! Expose subroutine Fortran to users of this DLL

!DEC$ ATTRIBUTES DLLEXPORT:: FORTRANTEST

integer int1

int1 = int1 + 1

return

end

For Steve Lionel, I have had a hell of a job to get the
above message into the form I wanted - I frequently got telescoped lines, added spaces etc - until I cut out
a block of text just before the
 
code.
ALso, as you see, this block is in BOLD when I have not specified it as such...please can you fix this?

0 Kudos
Steven_L_Intel1
Employee
1,561 Views
I'd like to fix it, but I can't - the forum software is messing up. I have reported it to the appropriate folks.

Steve
0 Kudos
isn-removed200637
1,561 Views
Got your message.
I have looked at your project files. It appears you are using the Intel compiler, of which I have no knowledge.
I had hoped to find Compaq Visual Fortran workspace (.DSW)
and project (.DSP) files in your zipped archive, but
found none, only .VFPROJ files, of which I have no
experience either. It appears that the Intel compiler is
a different beast.
I use the Compaq Visual Fortran compiler with Visual
studio, which uses the above-mentioned workspace and
project files.

It would appear that the Intel compiler does not recognise
the !DEC$ compiler directives. Also, the FORTRAN code I
posted was in free-format whereas I note your FORTRAN
code is aligned to start in column 7 as if it were fixed-
format. The Compaq compiler recognises code in files
ending .F or .F90 as being in free-format whereas files
ending .FOR are expected to be in fixed-format (starting
in column 7, comments start in column 1). What does the
Intel FORTRAN compiler expect? You will have to ask the
Intel compiler support for help on what their equivalents
are to the !DEC$ compiler directives. I think your problems
lie solely in this area. The FORTRAN and C++ code compile
fine using the VF compiler and MS Visual C++ compiler.

I attach a screen shot of the console (I have tweaked the
program to loop by accepting other +ve non-zero integers as input, terminating with a -ve number).



regards

Tony Richards
0 Kudos
isn-removed200637
1,561 Views
Well, I tried to attach a file, but the forum software failed miserably to do anything when I pressed the atttach files and post message button, It even failed to respond to the 'go back'edit' button after I had browsed to the file in the edit box. Here goes another try...
0 Kudos
Steven_L_Intel1
Employee
1,561 Views
The formatting problem should be fixed now. Sorry for the inconvenience.

Steve
0 Kudos
Reply