home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sgi / 11400 < prev    next >
Encoding:
Text File  |  1992-07-26  |  3.2 KB  |  101 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!uflorida!kluge!serss0!sumargoh
  2. From: sumargoh@serss0.fiu.edu (H. Sumargo)
  3. Newsgroups: comp.sys.sgi
  4. Subject: How to link C object with Fortran library?
  5. Keywords: Linkingobject with library
  6. Message-ID: <1765@kluge.fiu.edu>
  7. Date: 26 Jul 92 13:38:35 GMT
  8. Sender: news@kluge.fiu.edu
  9. Reply-To: sumargoh@fiu.edu
  10. Organization: FIU Electrical & Computer Engineering
  11. Lines: 88
  12.  
  13.  
  14. I guess that the above subject explained it all.  FYI, I would like to be able to
  15. write a program in C language that has the capability of doing complex math.  I know
  16. that C language does not support complex math (If I am not mistaken) without additional
  17. library, i.e. from research.att.com.  I tried to write a simple subroutine in ForTran
  18. to perform the task, complex math.  The program I wrote in ForTran was compiled and
  19. archived to a library called 'sub.a'.  In order to facilitate this library, I wrote
  20. a C program that calls this subroutine.  I compiled my C program and linked with the
  21. 'sub.a' library and the linker complained that it could not find some functions.  Below
  22. is the source code for the C and Fortran codes as well as the linker error.
  23.  
  24. C Program:
  25. #include <stdio.h>
  26. #include <math.h>
  27. struct complex
  28. {
  29.         float Real [5][3];
  30.         float Imag [5][3];
  31. };
  32. struct complex x, y;
  33. int i, j, k=5, l=3;
  34. main ()
  35. {
  36.         fprintf (stderr, "In C main program:\n");
  37.  
  38.         for (j=0; j<k; j++)
  39.         {
  40.                 for (i=0; i<l; i++)
  41.                 {
  42.                         x.Real [j][i] = cos(j*i) * cos(j*i) - sin(i*j) * sin(i*j);
  43.                         x.Imag [j][i] = - 2 * cos (i*j) * sin (j*i);
  44.                         fprintf(stderr, "%+f%+fj\t", x.Real [j][i], x.Imag [j][i]);
  45.                 }
  46.                 fprintf (stderr, "\n");
  47.         }
  48.         fprintf (stderr, "\n");
  49.         sub_(&k, &l, y.Real, y.Imag);
  50.         for (j=0; j<k; j++)
  51.         {
  52.                 for (i=0; i<l; i++)
  53.                         fprintf(stderr, "%+f%+fj\t", y.Real [j][i], y.Imag [j][i]);
  54.                  fprintf (stderr, "\n");
  55.         }
  56. }
  57.  
  58. ForTran subroutine:
  59.         subroutine sub (k1, k2, a1, a2)
  60.         complex a
  61.         real a1 (k2, k1), a2 (k2, k1), tmp
  62.         integer i, j, k1, k2
  63.  
  64.         write (6, *) 'In FORTRAN subroutine'
  65.          do j = 1, k1
  66.            do i = 1, k2
  67.               tmp = (j-1.0) * (i-1.0)
  68.               a = cos (tmp) - (0.0, 1.0) * sin (tmp)
  69.               a = a * a
  70.               a1 (i, j) = real (a)
  71.               a2 (i, j) = imag (a)
  72.            end do
  73.         end do
  74.         return
  75.         end
  76. -----------
  77.  
  78. Compiling the source code:
  79.         cc -c main.c
  80.         f77 -c sub.f
  81.  
  82. Generating ForTran library:
  83.         ar rc sub.a sub.o
  84.  
  85. Linking the C object with the ForTran library:
  86.         cc -O3 -o main main.o sub.a -lm
  87.         /usr/bin/ld:
  88.         Undefined:
  89.         s_wsle
  90.         do_lio
  91.         e_wsle
  92. -----------
  93.  
  94. Can someone please tell what I did wrong with the above procedures?  On the other hand,
  95. I would also like to know if there is a better way of writing a complex function in C
  96. language than the above programs so that I can put all the complex math that I need in
  97. a library.  Please email your response to sumargoh@fiu.edu.  Thank you very much in 
  98. advanced.
  99.  
  100. P.S.  Do I need to create a header file for the ForTran subroutine?
  101.