Re: Mixing Fortran and C (123 Views)
Occasional Contributor
Carola Bloemer_1
Posts: 2
Registered: ‎09-21-2000
Message 1 of 3 (123 Views)
Accepted Solution

Mixing Fortran and C

I have a program code mixing Fortran77 and C. I can compile each source file to an o-file using the aCC and Fortran 90 compiler but the linking of the program fails.

Here the code:

>>>>>>>>>> main.c: <<<<<<<<<<
int main(unsigned int argc,char **argv)
extern void halloc(void);
extern void hallof(void);
/* c calls fortran: */
/* c calls c: */
void halloc(void){ printf("hallo cn"); }

>>>>>>>>>> help.f: <<<<<<<<<<
subroutine hallof
print *,"HALLO F"
C.....fortran calls c:
call halloc()

I tried different ways to compile this test-program. Reading the online-manuals I discover the tip to link with "-lisamstub". This has no effect. Here how I tried to compile and link the program:
f90 -c help.f
aCC -c main.o
aCC -o test main.o help.f -lisamstub
Can anybody help me solving this problem?

Thanking you in advance.
Please use plain text.
Honored Contributor
Stefan Farrelly
Posts: 3,910
Registered: ‎10-23-1996
Message 2 of 3 (123 Views)

Re: Mixing Fortran and C

You havent specificed the error you get when trying to link it. Can you ?

Meanwhile you could try;
aCC -o test main.o help.f -l:isamstub

Im from Palmerston North, New Zealand, but somehow ended up in London...
Please use plain text.
Valued Contributor
Andy Bennett
Posts: 22
Registered: ‎10-24-1999
Message 3 of 3 (123 Views)

Re: Mixing Fortran and C

The problem with is that you've switched from using a C compiler (cc) to a C++ one (aCC). If you have no genuine C++ code it is probably unecessary to switch this compiler since cc is still a fully supported and current product. Your unresolved symbols should vanish by doing this alone. You may need libraries "-lF90 -lcl" in your link line to support the additional f90 runtime functions if your fortran code makes use of it.

The problem is caused by C++ mangling the real function names when it constructs code. By performing an "nm main.o" we can see what ld is really looking for:

halloc__Fv | 68|extern|entry |$CODE$
hallof__Fv | |undef |code |

As you can see, aCC has attached an "_Fv" denoting a (void) argument list. If you wish to use the functions as if they were C, you must 'extern "C"' them.

That is:

<< main.c >>


extern "C" void halloc(void);
extern "C" void hallof(void);

int main(unsigned int argc,char **argv)
/* c calls fortran: */
/* c calls c: */

void halloc(void){ printf("hallo cn"); }

$ f90 -c help.f
external subroutine HALLOF

6 Lines Compiled
$ aCC -c main.c
$ aCC -o test main.o help.o
$ ./test
hallo cnhallo cn$

For more about this, please see the "Mixing C++ with Other Languages" section of the aCC online programmers guide (run "aCC +help").
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation