# Undefined symbol error??

• 03-18-2005
Jedijacob
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; }```

--Jacob
• 03-18-2005
Dave Evans
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) ... }```

--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
• 03-18-2005
Salem
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
• 03-18-2005
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
• 03-18-2005
Dave Evans
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
• 03-18-2005
Jedijacob
Here is my definition of the struct.

Code:

```typedef struct _cmplxCDT {   double x;   double y; }cmplxCDT;```
• 03-18-2005
Jedijacob
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 :```
• 03-18-2005
Dave_Sinkula
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.
• 03-18-2005
Jedijacob
Oh, sorry..this is in my interface file and I forgot about it:

Code:

`typedef struct _cmplxCDT* Cmplx;`
• 03-18-2005
Dave_Sinkula
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 :```
• 03-18-2005
Jedijacob
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