[c] Passing a function

Discussion in 'OT Technology' started by RyanL, Mar 2, 2005.

  1. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    [c] Passing a function - Solved

    I writing a program for one of my cs labs where i have to pass a function.

    I can't figure it out for the life of me, here is a small example that was given in class but still isn't compiling for some reason.

    Code:
      
      #include <stdio.h>
      #include <math.h>
      
      double evalutate2(double f(), double);
      
      int
      main(void)
      {
      
       	   double sqrtvalue, sinvalue;
       	   sqrtvalue = evaluate2(sqrt, 12.5);
       	   printf("%f\n",sqrtvalue);
       	   sinvalue = evaluate2(sin, 0.5);
       	   printf("%f\n",sinvalue);
      
      }
      
      double evaluate2(double f(double f_arg), double pt1)
      {
       	   return (f(pt1));
      }
      
       
    Any light that could be shed on this would help so much.

    Oh, this is what the compiler spits at me:


    Code:
        gcc testfunction2.c
       testfunction2.c:25: warning: type mismatch with previous implicit declaration
       testfunction2.c:17: warning: previous implicit declaration of `evaluate2'
       testfunction2.c:25: warning: `evaluate2' was previously implicitly declared to return `int'
       /tmp/ccSumSKg.o(.text+0x1d): In function `main':
       : undefined reference to `sqrt'
       /tmp/ccSumSKg.o(.text+0x51): In function `main':
       : undefined reference to `sin'
       collect2: ld returned 1 exit status
       
     
    Last edited: Mar 3, 2005
  2. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    You simply spelled evaluate2 wrong when you declared it.

     
  3. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    no freaking way did i miss that... lol... however i am still getting this when i try to compile:
    Code:
     gcc testfunction2.c
     /tmp/ccdlcM6h.o(.text+0x1d): In function `main':
     : undefined reference to `sqrt'
     /tmp/ccdlcM6h.o(.text+0x49): In function `main':
     : undefined reference to `sin'
     collect2: ld returned 1 exit status
     
     
  4. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    change
    double evaluate2(double f(), double);

    to
    double evaluate2(double f(double), double);
     
  5. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    AHH!!! Seriously this stuff is driving me insane... I changed that, and it didn't change a thing a far as the compiler is concerned....

    I do appreciate the help though... any other suggestions?
     
  6. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    strange, it works on visual c++.
     
  7. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    This is C and not C++. I can't recall if that matters in this case or not, my C++ is a little rusty.

    This program is verbatium what the teachers example was in class and it won't compile...
     
  8. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    It would matter, in the sense that normally a C compiler won't recognize C++, however Visual C++ can compile both C and C++, as other modern compilers do. But let me try a C compiler.
     
  9. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    what about adding return 1;

    printf("%f\n",sinvalue);
    return 1;
    }
     
  10. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    gcc is only for C (g++ is for C++). It looks like he's supposed to use gcc.
     
  11. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    Hmm, if that's the case, I just tried it now with gcc, hopefully it works now.
     
  12. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    It seems to be a problem with the includes. If you declare a function called sqrt(double) and a function called sin(double), then it works just fine.
     
  13. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    it works in gcc (on my computer). I think the sqrt and sin is already declared in the math.h library.
     
  14. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    Well then, I am chalking it up to the computer system at school being borked. Thanks for the help guys.
     
  15. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Yeah, it would seem that it should work that way, since sqrt and sin are definitely both declared in the math.h library, but for some reason it doesn't on my computer or his school machine :dunno:
     
  16. R-Type

    R-Type The Bydo Empire must die!

    Joined:
    Aug 2, 2002
    Messages:
    1,049
    Likes Received:
    0
    Location:
    CT
    easy fix...gcc does not link libraries by default (except for libc), so

    gcc evaluate2.c -lm -o evaluate2

    should work fine (does here). -lm links libm(ath) which you need for sin and sqrt calls.. -o is the filename for the output executable
     
  17. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    would he also have to link the stdio.h library?
     
  18. R-Type

    R-Type The Bydo Empire must die!

    Joined:
    Aug 2, 2002
    Messages:
    1,049
    Likes Received:
    0
    Location:
    CT
    That would be part of glibc, which is linked implicitly by default when you invoke gcc...though adding -lc should not harm anything.
     
  19. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    R-Type, you were right with that, I talked to my TA today and thats what I was missing. Man can that be frustrating though, I knew the code was right...
     
  20. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Ah, that's what I figured it might be (since the error was during linking), but I couldn't figure out how to get it to include the math library.
     
  21. R-Type

    R-Type The Bydo Empire must die!

    Joined:
    Aug 2, 2002
    Messages:
    1,049
    Likes Received:
    0
    Location:
    CT
    Yeah. The nomenclature is usually to strip the 'lib' prefix from the library name. libm-x.y.z.so becomes -lm, libstdc++ becomes -lstdc++ etc.. :)
     

Share This Page