home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!uflorida!kluge!serss0!sumargoh
- From: sumargoh@serss0.fiu.edu (H. Sumargo)
- Newsgroups: comp.sys.sgi
- Subject: How to link C object with Fortran library?
- Keywords: Linkingobject with library
- Message-ID: <1765@kluge.fiu.edu>
- Date: 26 Jul 92 13:38:35 GMT
- Sender: news@kluge.fiu.edu
- Reply-To: sumargoh@fiu.edu
- Organization: FIU Electrical & Computer Engineering
- Lines: 88
-
-
- I guess that the above subject explained it all. FYI, I would like to be able to
- write a program in C language that has the capability of doing complex math. I know
- that C language does not support complex math (If I am not mistaken) without additional
- library, i.e. from research.att.com. I tried to write a simple subroutine in ForTran
- to perform the task, complex math. The program I wrote in ForTran was compiled and
- archived to a library called 'sub.a'. In order to facilitate this library, I wrote
- a C program that calls this subroutine. I compiled my C program and linked with the
- 'sub.a' library and the linker complained that it could not find some functions. Below
- is the source code for the C and Fortran codes as well as the linker error.
-
- C Program:
- #include <stdio.h>
- #include <math.h>
- struct complex
- {
- float Real [5][3];
- float Imag [5][3];
- };
- struct complex x, y;
- int i, j, k=5, l=3;
- main ()
- {
- fprintf (stderr, "In C main program:\n");
-
- for (j=0; j<k; j++)
- {
- for (i=0; i<l; i++)
- {
- x.Real [j][i] = cos(j*i) * cos(j*i) - sin(i*j) * sin(i*j);
- x.Imag [j][i] = - 2 * cos (i*j) * sin (j*i);
- fprintf(stderr, "%+f%+fj\t", x.Real [j][i], x.Imag [j][i]);
- }
- fprintf (stderr, "\n");
- }
- fprintf (stderr, "\n");
- sub_(&k, &l, y.Real, y.Imag);
- for (j=0; j<k; j++)
- {
- for (i=0; i<l; i++)
- fprintf(stderr, "%+f%+fj\t", y.Real [j][i], y.Imag [j][i]);
- fprintf (stderr, "\n");
- }
- }
-
- ForTran subroutine:
- subroutine sub (k1, k2, a1, a2)
- complex a
- real a1 (k2, k1), a2 (k2, k1), tmp
- integer i, j, k1, k2
-
- write (6, *) 'In FORTRAN subroutine'
- do j = 1, k1
- do i = 1, k2
- tmp = (j-1.0) * (i-1.0)
- a = cos (tmp) - (0.0, 1.0) * sin (tmp)
- a = a * a
- a1 (i, j) = real (a)
- a2 (i, j) = imag (a)
- end do
- end do
- return
- end
- -----------
-
- Compiling the source code:
- cc -c main.c
- f77 -c sub.f
-
- Generating ForTran library:
- ar rc sub.a sub.o
-
- Linking the C object with the ForTran library:
- cc -O3 -o main main.o sub.a -lm
- /usr/bin/ld:
- Undefined:
- s_wsle
- do_lio
- e_wsle
- -----------
-
- Can someone please tell what I did wrong with the above procedures? On the other hand,
- I would also like to know if there is a better way of writing a complex function in C
- language than the above programs so that I can put all the complex math that I need in
- a library. Please email your response to sumargoh@fiu.edu. Thank you very much in
- advanced.
-
- P.S. Do I need to create a header file for the ForTran subroutine?
-