Thread: collect2: ld returned 1 exit status error??

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    collect2: ld returned 1 exit status error??

    I have a pretty simple program, it just asks the user for 3 values and then those are put into the quadratic formula (only using the -b + part of it, no -b -), and then it returns it and prints out the answer. However, I'm getting an error when compiling that I've never seen before.

    "/tmp/cceIIpYr.o: In function `main':hw9.3A.c.text+0x84): undefined reference to `posQuadraticEquation'
    collect2: ld returned 1 exit status"

    hw9.3A.c is the name of the program. I'm compiling with this command:
    gcc -o hw9.3A hw9.3A.c -lm

    Here is the code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double posQuadracticEquation(int a, int b, int c);
    
    
    int main()
    {
            int a;
            int b;
            int c;
    
    
            printf("Please enter an integer for 'a'.\n");
            scanf("%d", &a);
    
    
            printf("Please enter an integer for 'b'.\n");
            scanf("%d", &b);
    
    
            printf("Please enter an integer for 'c'.\n");
            scanf("%d", &c);
    
    
            double answer = posQuadraticEquation(a, b, c);
    
    
            printf("With the values you entered, the positive portion of the Quadratic equation comes out to %lf\n", answer);
    
    
    }
    
    
    double posQuadracticEquation(int a, int b, int c)
    {
    double answer;
    
    
            answer = -b/(2*a) + sqrt(b*b - 4*a*c) / (2*a);
    
    
            return answer;
    
    
    }
    Any ideas?? Thanks!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    The compiler is right, there is a difference between posQuadracticEquation and posQuadraticEquation.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Ugh I always miss something small like that. Thank you!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Crap. For some reason I'm getting -nan for an answer regardless of what the user picks for a, b, and c. Anyone have any guesses as to why this is? I'm supposed to only return the POSITIVE portion of the quadratic formula. I was thinking this meant just the -b +, but I'm starting to think I only return it if it is a positive number inside of the square root? Hmm.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Well, if D < 0 then you won't have any real values.
    D = b^2 - 4ac
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Okay here's what I have now.

    Code:
    #include <stdio.h>#include <math.h>
    
    
    double posQuadraticEquation(int a, int b, int c);
    
    
    int main()
    {
            int a;
            int b;
            int c;
    
    
            printf("Please enter an integer for 'a'.\n");
            scanf("%d", &a);
    
    
            printf("Please enter an integer for 'b'.\n");
            scanf("%d", &b);
    
    
            printf("Please enter an integer for 'c'.\n");
            scanf("%d", &c);
    
    
            double answer = posQuadraticEquation(a, b, c);
    
    
            printf("With the values you entered, the positive portion of the Quadratic equation comes out to %lf\n", answer);
    
    
    }
    
    
    double posQuadraticEquation(int a, int b, int c)
    {
    double answer;
    double d = sqrt(b*b - 4*a*c);
    
    
    if(d<0)
    { return 0; }
    
    
    else
    {
            answer = -b/(2*a) + (d / (2*a));
    It returns a real number if values are entered where d > 0. But if values are entered that causes d < 0, then it just comes out as -nan. How would I go about making it so if d < 0, it displays something like "Variables result in an imaginary number". Like, if I put the printf inside of the IF statement, would it display that and then end the program? Or would it display that, AND THEN display what is in the printf in the main?

    Sorry it's a big question.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Notice that D = b^2 - 4ac, not D = sqrt(b^2 - 4ac); that is common math, I think. To indicate that there are no real values you could return a NAN. To test the return value use isnan. More information: https://www.gnu.org/s/libc/manual/ht...-Point-Classes
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Ah once again, something simple. I really need to focus on catching simple things like that. Thanks a lot for your help though, I appreciate it!

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    I tend to miss little things like that too often myself.
    Last edited by hauzer; 11-07-2011 at 06:04 PM. Reason: GRAAAAMARAGE
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Okay. I've been trying to figure this out but I'm out of ideas. This is the last thing I promise!

    Code:
    #include <stdio.h>#include <math.h>
    
    
    double posQuadraticEquation(int a, int b, int c);
    
    
    int main()
    {
            int a;
            int b;
            int c;
    
    
    
    
            printf("Please enter an integer for 'a'.\n");
            scanf("%d", &a);
    
    
            printf("Please enter an integer for 'b'.\n");
            scanf("%d", &b);
    
    
            printf("Please enter an integer for 'c'.\n");
            scanf("%d", &c);
    
    
            double answer = posQuadraticEquation(a, b, c);
    
    
            printf("With the values you entered, the positive portion of the Quadratic equation comes out to %lf\n", answer);
    
    
    }
    
    
    double posQuadraticEquation(int a, int b, int c)
    {
    double answer;
    double d = (b*b - 4*a*c);
    
    
    if(d<0)
    { printf("The integers you have entered result in an imaginary number. Please try again.\n");
    }
    
    
    else
    {
            answer = -b/(2*a) + (sqrt(d) / (2*a));
    
    
            return answer;
    }
    
    
    }

    Everything works properly except, if I put in numbers that make d < 0. It displays "The integers you have entered result in an imaginary number. Please try again." but then it goes back to the main and displays "With the values you entered, the positive portion of the Quadratic equation comes out to nan". I'm not sure how to get it to not display that...

  11. #11
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    The program will always call that last printf(), no matter what's the output of posQuadraticEquation. You shoudn't do any input/output in a function like that, instead you should test it's return value. I will quote myself..
    Quote Originally Posted by hauzer
    To indicate that there are no real values you could return a NAN. To test the return value use isnan. More information: https://www.gnu.org/s/libc/manual/ht...-Point-Classes
    You should do something like this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h> 
    
    double posQuadraticEquation(const double a, const double b, const double c)
    {
        double D;
         
        D = b*b - 4*a*c;
    
        if(D < 0) return NAN;
        else return (-b + sqrt(D)) / (2*a);
    }
    
    int main(int argc, char **argv)
    {
            double a, b, c;
            double answer;
    
            printf("Please enter 'a': ");
            scanf("%lf", &a);
            printf("Please enter 'b': ");
            scanf("%lf", &b);
            printf("Please enter 'c': ");
            scanf("%lf", &c);
     
            answer = posQuadraticEquation(a, b, c);
     
            if(isnan(answer))
                printf("The integers you have entered result in an imaginary number. Please try again.\n");
            else
                printf("With the values you entered, the positive portion of the Quadratic equation comes out to %lf\n", answer);
    
            return EXIT_SUCCESS;
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Ugh I finally got it. Thanks a lot for your help man, I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ld returned 1 exit status
    By madennn in forum C Programming
    Replies: 14
    Last Post: 03-29-2011, 11:26 AM
  2. error: collect2: ld returned 1 exit statu
    By tommantonela in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2009, 12:40 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-12-2008, 12:37 AM
  5. ERROR collect2: ld returned 1 exit status
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 12-04-2007, 12:20 PM