Undefined reference error

This is a discussion on Undefined reference error within the C Programming forums, part of the General Programming Boards category; hello, I get the follow error when i compile the code Code: fctr.c: In function ‘main’: fctr.c:17: warning: format ‘%ld’ ...

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

    Exclamation Undefined reference error

    hello,

    I get the follow error when i compile the code
    Code:
    fctr.c: In function ‘main’:
    fctr.c:17: warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘int’
    /tmp/ccfw9Xlk.o: In function `num_zeroes':
    fctr.c:(.text+0x33): undefined reference to `pow'
    fctr.c:(.text+0x3e): undefined reference to `floor'
    fctr.c:(.text+0x76): undefined reference to `pow'
    /tmp/ccfw9Xlk.o: In function `main':
    fctr.c:(.text+0x100): undefined reference to `numzeroes'
    collect2: ld returned 1 exit status
    My code is
    Code:
    #include <stdio.h>
    #include <math.h>
    long int num_zeroes(int num){
    	long int sum = 0;
    	int n;
    	for(n=0;num<pow(5,n);n++)
    		sum = sum + floor(num/pow(5,n));
    	return sum;
    }
    main(){
    	int cases,i;
    	long int num[100000];
    	scanf("%d",&cases);
    	for(i=0; i<cases; i++)
    		scanf("%ld",&num[i]);
    	for(i=0;i<cases;i++)
    		printf("%ld",num_zeroes(num[i]));
    }

    can any one guide me where i am going wrng
    Last edited by pshirishreddy; 08-02-2009 at 05:34 PM.

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    printf("%ld",numzeroes(num[i]));
    Your actual function name contains underscore.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    One of these is due to a spelling mistake, I'll let you guess which one.

    Most of the others may be due to not linking to the math library. With gcc, if you #include <math.h>, you must also compile with the -lm switch. I don't know how you do that with other compilers.

    The %ld thing may be just because that is more of an output (ie, printf, not scanf) parameter. Try %d.
    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

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Even after i correct the function name i end up with errors like undefined reference to
    /tmp/ccfw9Xlk.o: In function `num_zeroes':
    fctr.c.text+0x33): undefined reference to `pow'
    fctr.c.text+0x3e): undefined reference to `floor'
    fctr.c.text+0x76): undefined reference to `pow'

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Any idea on why the linking doesn't happen, though it solves the problem i am a little confused on why it doesnt compile the normal way. I never used the -lm switch before what does the switch do, i am unable to locate the switch functions even in the man pages ...

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    -lm tells the linker to include the math library when linking, so that it can find the definitions of those functions it's not finding.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    So, are there switches for every library like the math library ?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pshirishreddy View Post
    So, are there switches for every library like the math library ?
    -l just means "use the library that follows now". The math library is, for whatever reason, called "m". Hence -lm. The C runtime library (which is called "c") is linked in by default, but any other library you intend to use should be linked in. (For instance, if you were using OpenGL, you'd probably have -lopengl in your command line.)

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    thanks tabstop, where can i get the list of switches for the corresponding libraries ?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pshirishreddy View Post
    thanks tabstop, where can i get the list of switches for the corresponding libraries ?
    There aren't any. -l is the only switch there is, and you put the name of the library after the l. Presumably you know which libraries your program is intending to use, so you put those there.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pshirishreddy View Post
    thanks tabstop, where can i get the list of switches for the corresponding libraries ?
    There isn't one. Generally, if you are using a non-standard library, you have to look at some documentation first and hopefully it will say. For example:

    Mathematics: <math.h>

    but math.h is kind of an exception, since most people don't need instructions for pow().
    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

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    thank you..

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, 04: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, 04:27 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21