Home > Archive > Fortran > January 2008 > Need help on calling c functions from fortran programs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Need help on calling c functions from fortran programs
|
|
| ZelluX 2008-01-18, 4:31 am |
| I've written a small fortran program, which calls a c function and
exit.
I'm using Fortran PowerStation and Visual Studio 2005
under command prompt, i typed
> fl32 f.for
> cl c.c
it doesn't report any warning or error.
but when i link them together, it says:
> link f.obj c.obj
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of
other libs; use /NODEFAULTLIB:library
f.obj : error LNK2019: unresolved external symbol _NAMEAGE@4
referenced in function _main
f.exe : fatal error LNK1120: 1 unresolved externals
how to call c functions correctly?
thanks
Here are the two programs
------------- c.c ----------------
#include <string.h>
#include <stdio.h>
void show_(int *x) {
printf("%d\n", x);
}
------------- f.for ----------------
PROGRAM TEST
INTEGER VALUE
VALUE = 4
CALL SHOW(VALUE)
END
| |
|
| On 18 jan, 09:09, ZelluX <zel...@gmail.com> wrote:
> I've written a small fortran program, which calls a c function and
> exit.
> I'm using Fortran PowerStation and Visual Studio 2005
> under command prompt, i typed> fl32 f.for
>
> it doesn't report any warning or error.
>
> but when i link them together, it says:> link f.obj c.obj
>
> LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of
> other libs; use /NODEFAULTLIB:library
> f.obj : error LNK2019: unresolved external symbol _NAMEAGE@4
> referenced in function _main
> f.exe : fatal error LNK1120: 1 unresolved externals
>
> how to call c functions correctly?
> thanks
>
> Here are the two programs
> ------------- c.c ----------------
> #include <string.h>
> #include <stdio.h>
>
> void show_(int *x) {
> printf("%d\n", x);
>
> }
>
> ------------- f.for ----------------
> PROGRAM TEST
>
> INTEGER VALUE
> VALUE = 4
> CALL SHOW(VALUE)
> END
Your problem comes from the link phase. You need to mix Fortran and C
libraries.
I don't use neither the Fortran PowerStation nor the Visual Studio
2005. So I cannot give you good advices about how to proceed. With
compilers like g95, gfortran or ifort, one just need to link using the
Fortran command (g95 *.o for instance) because these compilers
automatically link with C libraries.
About the technique you used to write the routines, all seems OK
except that you print the address (x) instead of the value (*x). Of
course, your Fortran compiler is assumed to add an underscore at the
end of each routine name (this is often the case).
Your compiler is probably too old to include F2003 extensions like the
iso_c_binding module. With such module, the programming is safer and
you don't need to adopt Fortran conventions in the C part anymore :
------------- c.c ----------------
#include <string.h>
#include <stdio.h>
void show(int x) {
printf("%d\n", x);
}
------------- f.for ----------------
PROGRAM TEST
USE iso_c_binding
INTERFACE
SUBROUTINE show(x) BIND(C,name="show")
IMPORT c_int
INTEGER(c_int),VALUE :: x
END SUBROUTINE
END INTERFACE
INTEGER VALUE
VALUE = 4
CALL SHOW(VALUE)
END
| |
| ZelluX 2008-01-18, 8:13 am |
| Due to some reasons, I have to written f77 code.
Is there any similar ways?
On Jan 18, 7:10=A0pm, fj <francois.j...@irsn.fr> wrote:
> On 18 jan, 09:09, ZelluX <zel...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Your problem comes from the link phase. You need to mix Fortran and C
> libraries.
> I don't use =A0neither the Fortran PowerStation nor the Visual Studio
> 2005. So I cannot give you good advices about how to proceed. With
> compilers like g95, gfortran or ifort, one just need to link using the
> Fortran command (g95 *.o for instance) because these compilers
> automatically link with C libraries.
>
> About the technique you used to write the routines, all seems OK
> except that you print the address (x) instead of the value (*x). =A0Of
> course, your Fortran compiler is assumed to add an underscore at the
> end of each routine name (this is often the case).
>
> Your compiler is probably too old to include F2003 extensions like the
> iso_c_binding module. With such module, the programming is safer and
> you don't need to adopt Fortran conventions in the C part anymore :
>
> =A0------------- c.c ----------------
> #include <string.h>
> #include <stdio.h>
>
> void show(int x) {
> =A0 =A0 =A0 =A0 printf("%d\n", x);
>
> }
>
> ------------- f.for ----------------
> PROGRAM TEST
> USE iso_c_binding
> INTERFACE
> =A0 SUBROUTINE show(x) BIND(C,name=3D"show")
> =A0 =A0 IMPORT c_int
> =A0 =A0 INTEGER(c_int),VALUE :: x
> =A0 END SUBROUTINE
> END INTERFACE
> INTEGER VALUE
> VALUE =3D 4
> CALL SHOW(VALUE)
> END- Hide quoted text -
>
> - Show quoted text -
| |
|
| On 18 jan, 12:31, ZelluX <zel...@gmail.com> wrote:
> Due to some reasons, I have to written f77 code.
> Is there any similar ways?
No. In that case, what you have written is probably the best way to
achieve the coupling F77/C.
By may I suggest that F77 is no more the official FORTRAN standard
since 1990 ! And today, many F95 compilers exist which already
implement 2003 extensions like the iso_c_binding module. Moreover some
of them are free (g95, gfortran) or partly free (ifort on Linux,
Silverfrost of Windows).
|
|
|
|
|