Thread: undefined reference error, please help me

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up undefined reference error, please help me

    Hi

    I get following error while compiling using gcc in linux.

    /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    /tmp/ccQKd0uk.o: In function `initweights':
    weight.c.text+0xc6): undefined reference to `get_edge'
    /tmp/ccQKd0uk.o: In function `weightedSelect':
    weight.c.text+0x104): undefined reference to `ourrand'
    /tmp/ccQKd0uk.o: In function `deindex':
    weight.c.text+0x225): undefined reference to `sqrt'
    /tmp/ccQKd0uk.o: In function `weighted_graph':
    weight.c.text+0x5db): undefined reference to `set_edge'
    weight.c.text+0x648): undefined reference to `get_edge'
    weight.c.text+0x6f4): undefined reference to `get_edge'
    collect2: ld returned 1 exit status

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you don't post the code, any advice we give is just a supposition. We don't like that, we prefer to be on target and nip the problem in the bud.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by leo2008 View Post
    (.text+0x18): undefined reference to `main'
    You're doing something incredibly silly in your Makefile / compilation line most probably, like not specifying all your source files, libraries etc. The undefined references mean just that - the computer has no idea where to find get_edge etc. because you haven't told it where to find them.

    It's not really a "C" problem, it's a compiler problem - specifically a "user not using the compiler correctly" problem.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's "user not using the linker" correctly, actually. User is required to provide a main() function as the entry point for any C program. Linker complains if the main() function is not provided.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up

    Thanks for your quick reply. I actually did not include main.c file which throw the error. Now I have following error.

    error: conflicting types for âbzeroâ

    Here is the code on the line where error occurs, given below.

    extern void bzero(char *,int);


    What could be the reason here?

    Quote Originally Posted by leo2008 View Post
    Hi

    I get following error while compiling using gcc in linux.

    /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    /tmp/ccQKd0uk.o: In function `initweights':
    weight.c.text+0xc6): undefined reference to `get_edge'
    /tmp/ccQKd0uk.o: In function `weightedSelect':
    weight.c.text+0x104): undefined reference to `ourrand'
    /tmp/ccQKd0uk.o: In function `deindex':
    weight.c.text+0x225): undefined reference to `sqrt'
    /tmp/ccQKd0uk.o: In function `weighted_graph':
    weight.c.text+0x5db): undefined reference to `set_edge'
    weight.c.text+0x648): undefined reference to `get_edge'
    weight.c.text+0x6f4): undefined reference to `get_edge'
    collect2: ld returned 1 exit status

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Your "did not include main.c" sounds fishy....as you're not supposed to.
    List all your file names and what you are including from where.

    Also, show your makefile or the command you are compiling with.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The sqrt one at least should be easy to deal with. You need to include <math.h> in the source file that calls it, and then link it with the "-lm" command line argument when you compile.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up

    Quote Originally Posted by TheBigH View Post
    The sqrt one at least should be easy to deal with. You need to include <math.h> in the source file that calls it, and then link it with the "-lm" command line argument when you compile.
    I already included <math.h> in the source file as shown below. but it still gives me the same error.

    #include <string.h>
    #include <math.h>

    extern void bzero(char *,int);

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Why are you extern'ing the symbol when you're already including the header for it?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc error, undefined reference
    By drshmoo in forum C Programming
    Replies: 2
    Last Post: 04-04-2011, 09:33 PM
  2. Undefined reference error
    By pshirishreddy in forum C Programming
    Replies: 11
    Last Post: 08-02-2009, 06:34 PM
  3. Undefined Reference Error
    By NuNn in forum C Programming
    Replies: 12
    Last Post: 01-15-2009, 01:34 PM
  4. undefined reference error
    By gcctest in forum C Programming
    Replies: 3
    Last Post: 12-19-2008, 08:17 AM
  5. Linker Error (undefined reference)
    By Dae in forum C++ Programming
    Replies: 5
    Last Post: 07-15-2005, 03:50 PM