Thread: Undefined Reference Error

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, that wouldn't hurt.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Instead of <function> why not tell us which function it actually is? I suspect you're not linking with the math library.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    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

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    q:  q.o q.solver.o
    Shouldn't q.solver.o be qsolver.o?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by NuNn View Post
    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
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    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?
    Last edited by NuNn; 01-15-2009 at 12:01 PM.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by NuNn View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    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.............

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by NuNn View Post
    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."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Alrighty, thank you very much for your help. Otherwise I would have been sitting thinking for a while trying to figure it out haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM