CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   command to link fortran and c object (

phuang May 4, 2001 15:53

command to link fortran and c object
Please help me what is the command in PC to link fortran and c object to create an executive file. I have a program written in fortran and c.

andy May 4, 2001 17:22

Re: command to link fortran and c object
The program that performs the operation is usually called a linker. On unix the command is "ld". It used to be "link" on PCDOS many years ago but I do not know how to perform the task by pointing and clicking at windows these days.

In addition, you will have to make sure all the Fortran and C libraries are linked in, the Fortran calling C and vice-versa naming conventions are followed (usually by adding underscores) and the convention for passing Fortran strings is followed (Fortran passes a pointer and a length, C just the pointer - usually the lengths are collected in order and placed at the end of the argument list). Mixed I/O can be a problem on some systems. Some systems prefer main to be in Fortran while others in C. The task is not wholly straightforward the first time and varies from system to system.

clifford bradford May 4, 2001 23:47

Re: command to link fortran and c object
Yeah it's a pain in the ass. I had to do this once for visual C they recomended compiling individually then linking w/ visual C.

D.M. Lipinski May 5, 2001 03:27

Re: command to link fortran and c object
To be able to link Fortran and C, watch the following rules during compilation. 1. For Fortran sources: use compiler switch to add underscore to all external names. 2. For Fortran sources: use compile switch to generate all external names in lowercase. 3. Make sure that you pass arguments by reference (pointers) between C and Fortran. 4. Avoid passing character strings between Fortran and C. If you must do it, read the documentation on how your compilers pass (and expect) characters in the parameter list. Besides the character string, its length should often be specified.

Now you can link the object files with the linker.


John C. Chien May 5, 2001 21:22

Re: command to link fortran and c object
(1). I had looked into this kind of mixed language programming long time ago. But my conclusion was, try to avoid it by all means.

Kike May 10, 2001 06:06

Re: command to link fortran and c object
Dear PHuang

After you had read all the messages in this thread try this

(1) to compile your C source

cc -c c_source.c

(2) to link it with your fortran code

f77 f_source.f c_source.o

I agree with JCChien, avoid this

Best whishes


phuang May 17, 2001 15:47

Re: command to link fortran and c object
I can not avoid link c with fortran, because some cfd softwares are written in fortran and c. Normally they are linked by makefile in SGI. Can we create makefile for PC?

All times are GMT -4. The time now is 21:14.