Thread: Weirdest error message i've ever seen

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Weirdest error message i've ever seen

    /tmp/cckrJitj.o(.text+0x119): In function `main':
    : undefined reference to `pow'
    collect2: ld returned 1 exit status

    here is my code (it's ugly, and i know a lot of unnecessary steps due to debugging i've tried):

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
            int sevens = 0;
            int input = 0;
            int digits;
            int division = 10000000;
            int counter = 8;
            int x = 7;
            int go = 1;
            float power = 0;
            int mod = 1;
            int counter2 = 0;
            int y;
            int powerint = 0;
    
            printf("Welcome to the amazing 7 finder machine. Give me any number ");
            printf("and I can tell how many 7s are in it(Whole numbers only): ");
            scanf("%d", &input);
    
            while (go == 1)
            {
                    if (input / division >= 1)
                    {
                            digits = counter;
                            go = 0;
                    }
                    division /= 10;
                    counter--;
            }
    
            for (counter = 1; counter <= digits; counter++)
            {
                    y = digits - counter;
                    power = pow(10, y);
                    x = (input / power);
                    if (x == 7)
                    {
                            sevens++;
                    }
                    for (counter2 = digits; counter2 >= counter; counter2--)
                    {
                            mod *= 10;
                    }
    
                    input = (input % mod);
                    mod = 1;
            }
            printf("\nThere are %d sevens in your integer. Thanks!\n", sevens);
            return 0;
    }



    please help, my deadline is tonight at midnight!!!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You're missing a header file.

    [edit] no -- robwhit is right, you need to add -lm
    Last edited by MK27; 10-09-2008 at 03:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    gcc file.c -Wall -Werror -ansi -pedantic -lm

    pow is in the math library.

    ld is the linker. So since it got past the compiler, you know it's not a header file, probably. undefined reference means it can't find something that was referred to. In this case, it can't find the pow() function you were talking about. It automatically links with libc, but not libm, the math library. So it compiles, and then links with libc and your object file that it produced from your source file. Then it tries to resolve all the functions that are referred to in your object file. It can't find pow, so it stops and gives an error.
    Last edited by robwhit; 10-09-2008 at 04:03 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    i have the math.h header file. so what other header do i need. if i change the code slightly, by removing the nested for loop involving counter2, then i just get the error that i have an invalid operand for binary %, which makes sense, since power is a float, and % only can use integers. So that's y i tried to make an integer the same size. but now it gives me the pow error again. should i make the loop earlier and forgo the pow function completely?

    did i just answer my own question?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    no, shikaku13, no!

    just compile with -lm
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    oh. i didn't know what that meant. sorry. thank you. it's an assignment though, will my teacher think to do that?

    edit: it works now, but it doent read a 7 in the last digit, can anyone help me with that?
    Last edited by shikaku13; 10-09-2008 at 03:54 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hopefully your instuctor will figure it out after every single assignment fails to link.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you're in doubt, you can submit a makefile for him to use, but most likely he'll know what he is doing. You're fine.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by shikaku13 View Post
    oh. i didn't know what that meant. sorry. thank you. it's an assignment though, will my teacher think to do that?

    edit: it works now, but it doent read a 7 in the last digit, can anyone help me with that?
    Your mod just didn't scale high enough.
    Code:
    for (counter2 = digits; counter2 > counter; counter2--)
    makes it work.

    power and mod are pretty much the same throughout. So you might want to optimize things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weirdest Problem i've ever encountered!Please help me
    By chottachatri in forum C++ Programming
    Replies: 12
    Last Post: 05-25-2008, 11:14 AM
  2. Weirdest error with '==' operator.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2006, 06:30 PM
  3. weirdest logical error i've ever run into
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2003, 10:50 AM