Thread: undefined reference to (function)

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    undefined reference to (function)

    I just added a function (addToMsgQueue) to one of my sourcefiles (messaging.c) and to its header (messaging.h), compiled into a shared library, and now am trying to compile a third file - matchingengine.c - which includes messaging.h. Until I added this new function everything worked fine. Now my compiler gives me "undefined reference to 'addToMsgQueue' ". I cannot get it to compile and don't know where to look for anymore.

    here I what is I hope, the relevant code
    Code:
    here I define the function in messaging.c
    ---------------------------------------
    
    void addToMsgQueue(MsgQueue **aQueue, void *aMsg, int aType) {
    
    	MsgQueue *thisQueue = malloc(sizeof(*thisQueue));
    	thisQueue->type = aType;
    	thisQueue->msg = aMsg;
    	thisQueue->next = NULL;
    
    	if (*aQueue == NULL) {
    		*aQueue = thisQueue;
    	} else {
    		while ((*aQueue)->next != NULL) 
    			*aQueue = (*aQueue)->next;
    		(*aQueue)->next = thisQueue;
    	}
    }
    
    
    Here is the declaration in the messaging.h file
    -------------------------------------------
    
    int checkMessage (char *message);
    
    void addToMsgQueue(MsgQueue **aQueue, void *aMsg, int aType);
    
    struct order *parseOrderMsg(char *aMessageString);
    
    and here where it is called in matchingEngine.c
    ---------------------------------------------
    
    	MsgQueue *aQueue;
    	
    	struct fillReport *aFillReport;
    	
    	if (*sellBook == NULL)
    		return noMatches;
    	
    	while ((buyOrder.price >= (*sellBook)->order->price) && (buyOrder.actualSize > 0)){
    
    		size = buyOrder.actualSize - (*sellBook)->order->actualSize;
    		if (size == 0) {
    
    			aFillReport = malloc(sizeof(struct fillReport));
    			aFillReport->type = FILL_REPORT;
    			aFillReport->sellOrderID = (*sellBook)->order->ID;
    			aFillReport->buyOrderID = buyOrder.ID;
    			aFillReport->price = buyOrder.price;
    			aFillReport->tradedSize = buyOrder.actualSize;
    			addToMsgQueue(&aQueue, aFillReport, FILL_REPORT);
    		}
    Any ideas?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Are you linking with the shared library that you compiled?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by django View Post
    Any ideas?
    Yes, you are not linking correctly.
    Figure out how to link multiple files using your compiler.
    Edit: You need to link to the library as AndrewHunter stated.

    Tim S.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Never mind

    Jim

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    I just added a function (addToMsgQueue) to one of my sourcefiles (messaging.c) and to its header (messaging.h), compiled into a shared library, and now am trying to compile a third file - matchingengine.c - which includes messaging.h. Until I added this new function everything worked fine. Now my compiler gives me "undefined reference to 'addToMsgQueue' ". I cannot get it to compile and don't know where to look for anymore.
    Sure does sound like a linker error to me... Just like the others already noted.

    However; if it was linking before, there may an alternate explaination...
    Your new function has an error in it and your library did not recompile, meaning it's finding the library, but it's finding the old version... Turn your warnings on maximum, recompile the library on it's own... heed all warnings and errors as issues to be dealt with.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    It was linking before.
    I have warnings to maximum (on gcc -Wall).

    Come to think of it, everything that I used until now where structures that were in the messaging.h file, this is actually the first time that I try to call a function from messaging.c. So maybe something was not right all the time, but it only surfaces now...

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I got it. It was a linking error. I was misled by the fact that it never called something from the messaging.c file and therefore thought the linking was ok, which it was not. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function gives undefined reference bug
    By blue sam3 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2010, 07:56 AM
  2. undefined reference to a function
    By ChoCo in forum C Programming
    Replies: 1
    Last Post: 10-04-2009, 12:08 AM
  3. undefined reference to template function
    By Elkvis in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2009, 08:13 AM
  4. Undefined Reference to function?
    By fireonyx in forum C Programming
    Replies: 6
    Last Post: 03-17-2006, 06:15 PM
  5. undefined reference to a function
    By rodrigouy in forum C Programming
    Replies: 7
    Last Post: 01-17-2006, 06:47 AM