Thread: undefined reference

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    23

    undefined reference

    Hello,

    I have a problem. When trying to compile my sourcecode I get following warning:

    undefined reference to `add'

    This is where I get it:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "mytype.h"
    
    int main(int argc, char *argv[])
    {
        MyType ps = {0,0};
        add(3, &ps);
        ...
    This is the header:

    Code:
    typedef struct 
    {
        int counter;
        int lastindex;
        unsigned int codes[100];
    } MyType;
    
    void add(unsigned int code, MyType *ps);
    What did I get wrong? Both files are in the same directory.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to actually define the add function.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    It appears that you have included a function prototype for the function 'add', but no actual function. Hence the error 'undefined reference', since you are calling a function which you haven't included in your source code.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    Hello, I defined the function in a file called mytype.c which includes mytype.h.
    How do I get it from there?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Depends on what you're using to compile your code. You need to compile it along with your other code.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    Oh yeah, I have to compile all source files at once, that was my mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 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. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM