Thread: gcc error: undefined reference

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    52

    gcc error: undefined reference

    So I am attempting to clean up my makefile and have run into an issue.

    I am receiving the errors:

    parser.c:18: undefined reference to `parser1'
    parser.c:20: undefined reference to `parser2'

    And this is my makefile:

    Code:
    CC=gcc
    CFLAGS=-std=c99 -g3 -Wall
    
    parser: parser.o parsers.a
        $(CC) $(CFLAGS) -o parser parser.o parsers.a
    
    parser.o: parser.c parser.h
        $(CC) $(CFLAGS) -c parser.c
    
    parsers.a: parser1.a parser2.a parse_misc.o
        ar cr parsers.a parser1.a parser2.a parse_misc.o
    
    parser1.a: parser1.c parser1.h constants.h packet1.o
        $(CC) $(CFLAGS) -c parser1.c
        ar cr parser1.a parser1.o packet1.o
    
    parser2.a: parser2.c parser2.h constants.h packet2.o
        $(CC) $(CFLAGS) -c parser2.c
        ar cr parser2.a parser2.o packet2.o
    
    packet1.o: packet1.c packet1.h constants.h
        $(CC) $(CFLAGS) -c packet1.c
    
    
    packet2.o: packet2.c packet2.h constants.h
        $(CC) $(CFLAGS) -c packet2.c
    
    parse_misc.o: parse_misc.c parse_misc.h
        $(CC) $(CFLAGS) -c parse_misc.c
    
    clean:
        rm -f *.o *.a *~ parser
    I have an old makefile that works just fine so I know the problem is not with missing '#include's or some other code related problem, it is a problem with my makefile.

  2. #2
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    An alternate option that I choose to do is to compile my source codes manually. A makefile should automate many of the compiling for you. However it could get too complex so "keep it simple silly" is what I hear.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Tien Nguyen View Post
    An alternate option that I choose to do is to compile my source codes manually. A makefile should automate many of the compiling for you. However it could get too complex so "keep it simple silly" is what I hear.
    You are completely useless. All you are doing is wasting people's time. Is that what jesus would do? Your code is so terrible and your jesus-crap was so idiotic that I think you're just a troll. Added to ignore list.

    jdodle's problem is presumably related to creating a nested archive, an archive of archives. He should either create a "thin" archive (if that fits his needs, but this does not actually add the object code itself to the archive but instead references the original object files) or extract the files and add the object files themselves to the final archive (or just add the original object files since they are presumably still around).

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Quote Originally Posted by algorism View Post
    You are completely useless. All you are doing is wasting people's time. Is that what jesus would do? Your code is so terrible and your jesus-crap was so idiotic that I think you're just a troll. Added to ignore list.
    A little over the top... but thanks for putting a troll in its place.

    To be honest I'm not doing any archiving, I simply wanted to reduce the number of characters per line to avoid text wrapping and improve readability. I think this is the best way to do it, though if anyone has a better suggestion I'm all ears. The way I had it originally, each object file had its own target but then the executable was this massive multiline dependency list and a linking list that was just as long. I will probably reduce the number of targets I have but I wanted to start out fairly verbose to try and make bugs in the Makefile more transparent. The reason I'm creating libraries is to join multiple object files into one file, I couldn't find any other way to do that. Other than that... I would say it is actually pretty simple for having 13 source/header files.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    First, use the 'nm' utility to find out what symbols are actually defined.
    Eg
    Code:
    $ nm --defined-only /usr/lib/libSDL.a
    SDL.o:
    0000000000000160 T SDL_Init
    0000000000000000 T SDL_InitSubSystem
    00000000000002c0 T SDL_Linked_Version
    00000000000002a0 T SDL_Quit
    00000000000001b0 T SDL_QuitSubSystem
    0000000000000280 T SDL_WasInit
    0000000000000000 b SDL_initialized
    0000000000000004 b ticks_started
    Second, your naming of libraries is suspect.
    If the basename of the library is 'foo', then the actual name of the file should be libfoo.a
    When trying to link against that library using the compiler, the command line would include the argument -lfoo
    That is, a lower case 'L' followed by the basename of the library. The compiler knows to automatically add the lib prefix and the .a suffix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Quote Originally Posted by Salem View Post
    First, use the 'nm' utility to find out what symbols are actually defined.
    Great tool tip, this is awesome.

    Quote Originally Posted by Salem View Post
    Second, your naming of libraries is suspect.
    Works great after switching to linker flags and proper naming conventions. I had originally done it this way but I now realize I forgot to start the name with 'lib' so I thought I would just name them directly without using flags; obviously that didn't work. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: undefined reference
    By 9erNumber16 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2012, 07:27 PM
  2. undefined reference error, please help me
    By leo2008 in forum C Programming
    Replies: 8
    Last Post: 03-27-2012, 03:50 AM
  3. Undefined reference error
    By pshirishreddy in forum C Programming
    Replies: 11
    Last Post: 08-02-2009, 06:34 PM
  4. Undefined Reference Error
    By NuNn in forum C Programming
    Replies: 12
    Last Post: 01-15-2009, 01:34 PM
  5. undefined reference error
    By gcctest in forum C Programming
    Replies: 3
    Last Post: 12-19-2008, 08:17 AM

Tags for this Thread