Thread: Undefined Reference to Existing Library

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

    Undefined Reference to Existing Library

    I have to solve something in C for school, but I'm stuck and I can't solve this problem.

    I have this .c file where I call a function

    Code:
    lista = lista_criar(free);
    The function "lista_criar" is defined in "listas.h" and "listas.c".
    I have the include in the source file (#include "listas.h") and this header is in the same directory as the source, but when I try to compile i get this error:

    Code:
    cc -o prog main.o debug.o  
    main.o: In function `main':
    /home/sparkt/PA/Ficha3/ex3/main.c:79: undefined reference to `lista_criar'
    collect2: ld returned 1 exit status
    make: *** [prog] Error 1
    I also modified my makefile to this:

    Code:
    (...)
    
    #Dependencies
    main.o: main.c debug.h listas.h listas.c #${PROGRAM_OPT}.h
    ${PROGRAM_OPT}.o: ${PROGRAM_OPT}.c ${PROGRAM_OPT}.h
    
    debug.o: debug.c debug.h
    semaforos.o: semaforos.c semaforos.h
    
    (...)
    And still can't compile the code :s
    Hope someone can help me figure out what I'm missing!
    Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your function is in listas.c, you need to compile that file too.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Quote Originally Posted by tabstop View Post
    If your function is in listas.c, you need to compile that file too.
    I have listas.c and listas.h... I've added both to the makefile because i'm new to using makefiles and i was not sure what to add...

    I'm not sure I understand... I should compile listas.c and main.c somehow "together"?

    One more thing i've remembered, listas.h as a structure defined too, and i don't get any error while using the structure... the compiler recognizes it.
    But I do get that error when i call the lista_criar() function...

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    I guess i've figured it out... I've also edited this line in the makefile

    Code:
    PROGRAM_OBJS=main.o debug.o listas.c
    Seems like it's compiling listas.c like you said and not giving me that error!
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 01:00 AM
  5. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM