Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

Mixing Fortran and C

SOLVED
Go to solution
Highlighted
Carola Bloemer_1
Occasional Contributor

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: <<<<<<<<<<
#include
int main(unsigned int argc,char **argv)
{
extern void halloc(void);
extern void hallof(void);
/* c calls fortran: */
hallof();
/* c calls c: */
halloc();
}
void halloc(void){ printf("hallo cn"); }

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

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.
2 REPLIES
Stefan Farrelly
Honored Contributor

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...
Andy Bennett
Valued Contributor
Solution

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 >>

#include

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

int main(unsigned int argc,char **argv)
{
/* c calls fortran: */
hallof();
/* c calls c: */
halloc();
}

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

$ f90 -c help.f
help.f
external subroutine HALLOF

6 Lines Compiled
$ aCC -c main.c
$ aCC -o test main.o help.o
$ ./test
HALLO F
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").