Undefined symbol error??

This is a discussion on Undefined symbol error?? within the C Programming forums, part of the General Programming Boards category; Hi, I am doing a program that does all types of stuff with complex numbers. I think I have the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    Undefined symbol error??

    Hi, I am doing a program that does all types of stuff with complex numbers. I think I have the code written correctly but keep getting an Undefined symbol error. It tells me sqrt and arctan and gives the file complex.o . I have #include <math.h> in my complex.c file and am using a makefile with the LIB of -lm. And everywhere I look, it says to do these things but they are already done. What could be wrong? I posted the code for the functions that use sqrt and arctan and maybe my syntax is just wrong.

    Code:
    #include <math.h>
              .
              .
              .
    double MagCmplx(Cmplx z)
    {
       /* Write this function */
      double m;
    
      m = sqrt(((z->x)*(z->x)) + ((z->y)*(z->y)));
    return m;
    }
    
    double AngleCmplx(Cmplx z)
    {
       /* Write this function */
      double theta;
      if((z->y)==0 && (z->x)>0)
        theta = 0;
      else if((z->y)==0 && (z->x)<0)
        theta = PI * (-1);
      else
        theta = arctan((z->x),(z->y));
    
    return theta;
    }
    Please help!!! Thanks so much!

    --Jacob

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Jedijacob

    Code:
    #include <math.h>
              .
              .
              .
    double MagCmplx(Cmplx z)
    {
    ...
    
      m = sqrt(((z->x)*(z->x)) + ((z->y)*(z->y)));
    ...
    }
    
    double AngleCmplx(Cmplx z)
    {
    ...
      if((z->y)==0 && (z->x)>0)
    ...
    }
    Please help!!! Thanks so much!

    --Jacob
    If z is a struct, its members are accessed with the '.' operator, not the '->' operator. So you would have something like this:

    Code:
     m = sqrt((z.x * z.x) + (z.y * z.y));
    and

    Code:
      if((z.y == 0) && (z.x > 0))
    Regards,

    Dave

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    You're probably specifying "-lm" in the wrong place. Searching libraries is order sensitive.

    Example
    gcc -lm prog.c
    does not work

    gcc prog.c -lm
    does work
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    It's a linked list. We are studying unordered linked-list ADT's. Therefore, I am pretty sure we use the '->' instead of the '.' When I asked my instructor about the error he said that I probably capitalized it wrongly or misspelled it on accident or something like that but I don't see anything wrong with it at all!

    --Jacob

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Jedijacob
    It's a linked list. We are studying unordered linked-list ADT's. Therefore, I am pretty sure we use the '->' instead of the '.' When I asked my instructor about the error he said that I probably capitalized it wrongly or misspelled it on accident or something like that but I don't see anything wrong with it at all!

    --Jacob
    I was assuming that Cmplx is a struct. (That's why I said, "If Cmplx is a struct.")

    If it is a pointer to struct (has been typedef'ed to a pointer to struct), then you use '->'; If it is a struct (or has been typedef'ed to a struct) you use '.'

    Since you didn't get compile errors complaining about struct operations, I see that my previous reply was not helpful. Sorry

    I will note that arctan() is not part of the C standard libary, and is not prototyped in C standard header <math.h>

    Unless you have defined this function somewhere, I suggest that maybe you could use the standard library function atan2(). (A final note: if you do mean to use atan2(), the order of arguments for atan2() is y, x.)


    *edit* For my own edification, I would like to see the definition of Cmplx that you are using. Thanks


    Regards,

    Dave
    Last edited by Dave Evans; 03-18-2005 at 11:23 AM. Reason: Note about seeing definition of Cmplx

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Here is my definition of the struct.

    Code:
    typedef struct _cmplxCDT
    {
       double x;
       double y;
    }cmplxCDT;

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Also, here is my makefile which I would think would be the problem but everything looks fine to me:

    Code:
    # Source, Executable, Includes, Library Defines
    INCL   = complex.h
    SRC    = complex.c complexfun.c
    OBJ    = $(SRC:.c=.o)
    LIBS   = -lm
    EXE    = complexfun
    
    # Compiler
    CC     = gcc
    
    # Compile and Assemble C Source Files into Object Files
    .o:.c
            $(CC) -c  $(SRC)
    # Link all Object Files with external Libraries into Binaries
    $(EXE): $(OBJ)
            $(CC) -o $(EXE) $(OBJ) $(LIB)
    
    # Objects depend on these Libraries
    $(OBJ): $(INCL)
    
    # Create a dbx Capable Executable with flags turned on
    debug:
            $(CC) -Wall -g -DDEBUG -o $(EXE) $(LIBS)  $(SRC)
    
    # Clean Up Objects, Exectuables, Dumps out of source directory
    clean:
            rm -f $(OBJ) $(EXE) core
    
    .SILENT :

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Dave Evans
    I would like to see the definition of Cmplx that you are using. Thanks
    Quote Originally Posted by Jedijacob
    Here is my definition of the struct.

    Code:
    typedef struct _cmplxCDT
    {
       double x;
       double y;
    }cmplxCDT;
    That's not Cmplx, it's cmplxCDT.
    Last edited by Dave_Sinkula; 03-18-2005 at 03:39 PM. Reason: Adding this: now I'm curious too.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Oh, sorry..this is in my interface file and I forgot about it:

    Code:
    typedef struct _cmplxCDT* Cmplx;

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    # Source, Executable, Includes, Library Defines
    INCL   = complex.h
    SRC    = complex.c complexfun.c
    OBJ    = $(SRC:.c=.o)
    LIBS   = -lm
    EXE    = complexfun
    
    # Compiler
    CC     = gcc
    
    # Compile and Assemble C Source Files into Object Files
    .o:.c
            $(CC) -c  $(SRC)
    # Link all Object Files with external Libraries into Binaries
    $(EXE): $(OBJ)
            $(CC) -o $(EXE) $(OBJ) $(LIB)
    
    # Objects depend on these Libraries
    $(OBJ): $(INCL)
    
    # Create a dbx Capable Executable with flags turned on
    debug:
            $(CC) -Wall -g -DDEBUG -o $(EXE) $(LIBS)  $(SRC)
    
    # Clean Up Objects, Exectuables, Dumps out of source directory
    clean:
            rm -f $(OBJ) $(EXE) core
    
    .SILENT :
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Thanks a lot! It was that simple and I didn't even see it! Thanks for all your help. I might ask again later for some actual code help when I probably see my errors hehe! Thanks soo much again!!!

    --Jacob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21