# Undefined Reference Error

• 01-15-2009
NuNn
Undefined Reference Error
I am getting an Undefined reference to <function>, here is my code:

Code:

```#include <stdio.h> #include "qsolver.h" int main() {         double a, b, c, x1, x2;         int qret;         while(1) {                 printf("Enter variables for quadratic equation (type: \n");                 scanf("%lf %lf %lf", &a, &b, &c);                 if((qret = qsolver(a, b, c, &x1, &x2)) != 0) {                         printf("Error Value:  %d\n", qret);                 }         }         return 0; } ****************************************************************************** #include <stdio.h> #include <math.h> #include "qsolver.h" int qsolver(double a, double b, double c, double* x1, double* x2) {         //Is the user giving an input which would divide by zero?         if(a == 0) {                 printf("Input Not A Quadratic; Cannot Have Value Of '0' For A\n");                 return 1;         }         //Is the user giving and input which would try to compute a square root of a negative number.         else if( (4*a*c) > pow(b,2) ) {                 printf("There Are No Real Solutions; B^2 Is Less Than (4*A*C)\n");                 return 2;         }         //The user input is valid, solve accordingly         else {                 *x1 = (-b + sqrt(pow(b,2) - (4*a*c)))/(2*a);                 *x2 = (-b - sqrt(pow(b,2) - (4*a*c)))/(2*a);                                 return 0;         } } ***************************************************************************************** #ifndef QSOLVER_H #define QSOLVER_H int qsolver(double, double, double, double *, double *); #endif```
• 01-15-2009
tabstop
You'll have to compile all your .c files and link them together. (Most people don't actually run the linker separately; usually your compiler will do it for you, but you have to compile your .c files together.)
• 01-15-2009
NuNn
I have a Makefile I am using to link the files together but I am still getting the error. If you would like I can post that also
• 01-15-2009
tabstop
Well, that wouldn't hurt.
• 01-15-2009
brewbuck
Instead of <function> why not tell us which function it actually is? I suspect you're not linking with the math library.
• 01-15-2009
NuNn
The function is qsolver; here is the code to the makefile also which I believe links the math library:

Code:

```CC = gcc CFLAGS= -Wall -pedantic q:  q.o q.solver.o         \${CC} -o q q.o qsolver.o -lm q.o:  q.s         \${CC} -c q.s qsolver.o:  qsolver.s         \${CC} -c qsolver.s q.s:  q.c qsolver.h         \${CC} \${CFLAGS} -s q.c qsolver.s:  qsolver.c qsolver.h         \${CC} \${CFLAGS} -s qsolver.c clean:         rm -f *.o *.s q core```
• 01-15-2009
rags_to_riches
Code:

`q:  q.o q.solver.o`
Shouldn't q.solver.o be qsolver.o?
• 01-15-2009
brewbuck
Quote:

Originally Posted by NuNn
The function is qsolver; here is the code to the makefile also which I believe links the math library:

Well, I think your problem is in your makefile. Why are you compiling to ASSEMBLY first? Also, you say that "q" depends on something called "q.solver.o", yet you link with qsolver.o -- that means that qsolver.o will not get rebuilt because it's not listed as a dependency.

Surely your linker is also printing some error like "Can't find qsolver.o"

Try this:

Code:

```CC = gcc CFLAGS= -Wall -pedantic q:  q.o qsolver.o         \$(CC) -o \$@ \$^ -lm q.o:  q.c qsolver.o:  qsolver.c clean:         rm -f *.o q core```
• 01-15-2009
NuNn
Yep, typo on my part with the q.solver.o

brewbuck: Thank you that fixed the problem. Could you tell me what the "\$@ \$^" symbols mean?
• 01-15-2009
brewbuck
Quote:

Originally Posted by NuNn
Yep, typo on my part but the "undefined reference" still remains

So you admit the makefile you posted is not the actual makefile. I don't want to waste my time speculating when you won't show the real stuff.
• 01-15-2009
NuNn
Brewbuck what are you talking about????? That was my actual makefile. No need to get offensive here. I was thanking you for fixing my problem.............
• 01-15-2009
brewbuck
Quote:

Originally Posted by NuNn
Brewbuck what are you talking about????? That was my actual makefile. No need to get offensive here. I was thanking you for fixing my problem.............

I misunderstood you. I thought you meant you made a typo while retyping the makefile to post to the forum, and I hate it when people retype instead of copy-pasting. Sorry.

The special variable \$@ means "the target", i.e. the thing on the left side of the colon. The variable \$^ means "the dependencies," i.e. everything to the right of the colon. It makes maintenance of your makefile easier, because when you add a dependency you don't have to update the rule.

There are other specials, like \$? which means "only the dependencies which have actually changed."
• 01-15-2009
NuNn
Alrighty, thank you very much for your help. Otherwise I would have been sitting thinking for a while trying to figure it out haha.