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

Compiling C and Fortran Together (VS2005, Intel Fortran 9.1)

acobulareddy
Beginner
680 Views

Hi, I have a small C and Fortran programs.

C-Prog
--------
#include
#include

void CHGSTR(char str1[], char str2[])
{
int i;
char* cstr1 = "STR1ModifiedinC";
char* cstr2 = "String 2 Modified in C";
printf("\nIn C function: strings passed to C - %s, %s\n",str1,str2);
i=0;
while ( ( str1[i++] = *cstr1++ ) != '\0' ); // filling str1
i=0;
while ( ( str2[i++] = *cstr2++ ) != '\0' ); // filling str2
printf("\nIn C function: strings modified in C - %s, %s\n",str1,str2);
}

FORTRAN Prog
----------------
PROGRAM main
CHARACTER*15 string1
CHARACTER*50 string2
string1 = "Fortran String1"
string2 = "Fortran String 2 little lengthy"
write(*,*)"REDDYO - FORTRAN - BEFORE C - ",string1,string2
CALL ChgStr(string1,string2)
write(*,*)"REDDYO - FORTRAN - AFTER C - ",string1,string2
END PROGRAM main

If I compile those two from Intel Fortran Compiler 9.1 prompt I have no issues. where as I would like to work with these two in VS2005 User Interface.

I have VS2005 for C++ and Intel fortran 9.1 for Fortran.

I have created a static library for chgstr.c

I have created a Console Application for Fortran and in project properties -> Linker -> inputs -> Additional Dependencies I have added C Static Libray.

While linking I am finding the following error.

1>------ Build started: Project: fortexe, Configuration: Debug Win32 ------
1>Linking...
1>fmain.obj : error LNK2019: unresolved external symbol _CHGSTR referenced in function _MAIN__
1>Debug/fortexe.exe : fatal error LNK1120: 1 unresolved externals

Please let me know Whether I am doing wrong or is there something else that I have to do or VS2005 IDE wont support linking to Intel Fortran 9.1

Thanks,
Chandra

0 Kudos
4 Replies
Les_Neilson
Valued Contributor II
681 Views
Hello Chandra
If you look in the Fortran documentation under
Building Applications -> Programmingwith Mixed Languages -> Handling Data Types in Mixed-Language Programming -> Handling Character Strings
you will find the following
"By default, Intel Fortran passes a hidden length argument for strings. The hidden length argument consists of an unsigned 4-byte integer (IA-32 systems) or unsigned 8-byte integer (Intel EM64T and Itanium-based systems), always passed by value, added to the end of the argument list. You can alter the default way strings are passed by using attributes."
You are missing the "hidden length" arguments.
Also C expects strings to be NULL terminated. You will need to add the null terminator in yourFortran codeto strings passed from Fortran to C and remove the null terminator on string passed from C to Fortran.

Les
0 Kudos
acobulareddy
Beginner
681 Views
Thanks for your reply. But I dont think that is an issue. Because when I do compiling like
cl -c chgstr.c (for creating obj file)
lib /out chgstr.lib chgstr.obj (for creating static library from object file)
from visual C++ command prompt that is working find.
Now I have the libray
From Intel Visual Fortran Command Prompt I have executed the following command
ifort /names:lowercase /extend_source chgstr.lib fmain.f
This is creating me fmain.exe executable
while executing this it is functioning well.
So I am thinking there is something wrong with the procedure I am linking the library
From VS while compiling fortran code it is clearly mentioning the error as
unresolved external symbol _CHGSTR referenced in function _MAIN__
This tells that LIBRARY is not refered. If the problem is with hidden arguments it says some thing like somany bytes expected and somany bytes recived.
Please check the procedure I have followed to link a static library and let me know whether that procedure is correct.
Thanks,
Chandra.
Quoting - Les Neilson
Hello Chandra
If you look in the Fortran documentation under
Building Applications -> Programmingwith Mixed Languages -> Handling Data Types in Mixed-Language Programming -> Handling Character Strings
you will find the following
"By default, Intel Fortran passes a hidden length argument for strings. The hidden length argument consists of an unsigned 4-byte integer (IA-32 systems) or unsigned 8-byte integer (Intel EM64T and Itanium-based systems), always passed by value, added to the end of the argument list. You can alter the default way strings are passed by using attributes."
You are missing the "hidden length" arguments.
Also C expects strings to be NULL terminated. You will need to add the null terminator in yourFortran codeto strings passed from Fortran to C and remove the null terminator on string passed from C to Fortran.

Les

0 Kudos
TimP
Honored Contributor III
681 Views

This is one situation where there is value in upgrading to a compiler recent enough to support ISO C interop. Among the possibilities not discussed is that the function name is spelled in lower case in the C code, while, as shown, it is upper case with the Fortran options chosen. dumpbin, or equivalent, may be used to check the .obj files for consistency. Those differences must be reconciled, with too many options available, the only portable one being the ISO C interop.

I used to use either /Qlowercase on the Fortran side, or e.g. -Dfunction=FUNCTION on the C compilation, but that would have been a minority preference.

Another annoyance is that CL supports only C89 without C++ name mangling. ICL doesn't share that limitation. For full interoperability with VC, it may be necessary to use extern "C" even in C code.

0 Kudos
acobulareddy
Beginner
681 Views
Thanks for your reply. Let me talk about the program I posted.
I have different case calling in Fortran but irrespective of the calling case Fortran considers it as UPPER CASE and My C Program is having UPPER CASE as function name. So no issue of the case problem here
More over, I would like to know, How to Set this Case Option in VS2005 IDE for FORTRAN (not a command line argument)
I also assume the way I am adding static library to main Fortran Program through VS2005 IDE is wrong because if I add this library the command should be like /extend_source:LIBRARYNAME where as I didnt find that project setting anywhere. when I added library as the procedure I have mentioned it is adding like /SUBSYSTEM:LIBRARYNAME.
So I strongly feel that I am adding library wrongly to the VS2005 IDE of Fortran.
One more thing I would like to know whether Intel Fortran 9.1 is capable of getting handled in VS2005 for Mixed Language programming.
Regards,
Chandra.
Quoting - tim18

This is one situation where there is value in upgrading to a compiler recent enough to support ISO C interop. Among the possibilities not discussed is that the function name is spelled in lower case in the C code, while, as shown, it is upper case with the Fortran options chosen. dumpbin, or equivalent, may be used to check the .obj files for consistency. Those differences must be reconciled, with too many options available, the only portable one being the ISO C interop.

I used to use either /Qlowercase on the Fortran side, or e.g. -Dfunction=FUNCTION on the C compilation, but that would have been a minority preference.

Another annoyance is that CL supports only C89 without C++ name mangling. ICL doesn't share that limitation. For full interoperability with VC, it may be necessary to use extern "C" even in C code.

0 Kudos
Reply