Thread: Linker error

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Linker error

    Hello.I am working on a simple math project using C.

    I have these three files:

    queue.h
    Code:
    #include <stdlib.h>
    
    /* Simply linked lists */
    typedef struct ListNode_tag {
       int data;
       struct ListNode_tag * next;
    } ListNode;
    
    
    /* Type queue */
    typedef struct {
       ListNode * first;
       ListNode * last;
    } queue;
    
    /* Empty queue */
    const queue queueEmpty = { NULL, NULL };
    
    /* Insert */
    void queueInsert (queue * qp, int t);
    
    /* Remove */
    int queueRemove (queue * qp);
    
    /* Head */
    int queueHead (queue q);
    queue.c
    Code:
    #include <stdlib.h>
    
    /* Simply linked lists */
    typedef struct ListNode_tag {
       int data;
       struct ListNode_tag * next;
    } ListNode;
    
    
    /* Type queue */
    typedef struct {
       ListNode * first;
       ListNode * last;
    } queue;
    
    /* Empty queue */
    const queue queueEmpty = { NULL, NULL };
    
    /* Insert */
    void queueInsert (queue * qp, int t);
    
    /* Remove */
    int queueRemove (queue * qp);
    
    /* Head */
    int queueHead (queue q);
    main.c
    Code:
    #include "queue.h"
    
    int main()
    {
    	//queue q = queueEmpty;
    	return 1;
    }
    When I compile the project I get
    fatal error LNK1169: one or more multiply defined symbols found

    Can you help?Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Queue.h and queue.c looks identical. Not your real code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Right, the way the muiltiple files and the linking works is, you need to have too the function prototypes in the .h file and the function definiations in the .c file. In your case, you have the same copy of the .h file in the .c file which is why yoiu get the muiltiply defined symbols. Error.

    ssharish

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2
    Wrong copy paste sorry...

    queue.c
    Code:
    #include <stdio.h>
    #include "queue.h"
    /* Insert */
    void queueInsert (queue * qp, int t)
    {
       ListNode * n = (ListNode *) malloc(sizeof(ListNode));
    
       /* Check if malloc succeeded */
       if (n == NULL) {
          fprintf(stderr, "Out of memory\n");
          exit(1);
       }
    
       /* Copy the data */
       n->data = t;
       n->next = NULL;
    
       /* If the queue was empty, just add this one element */
       if (qp->last == NULL)
          qp->first = qp->last = n;
    
       /* Otherwise, add the new element at the end */
       else {
          qp->last->next = n;
          qp->last = n;
       }
    }
    
    /* Remove */
    int queueRemove (queue * qp)
    {
       ListNode * n;
       int result;
    
       /* Error, if the queue was empty */
       if (qp->first == NULL) {
          fprintf(stderr, "Nothing to remove from an empty queue\n");
          exit(1);
       }
    
       /* Remove and free the first element */
          n = qp->first;
       result = qp->first->data;
       qp->first = qp->first->next;
       free(n);
    
       /* If the queue is now empty, set the 'last' pointer too */
       if (qp->first == NULL)
          qp->last = NULL;
       
       return result;
    }
    
    
    /* Head */
    int queueHead (queue q)
    {
       /* Error, if the queue was empty */
       
       if (q.first == NULL) {
          fprintf(stderr, "Nothing to see in an empty queue\n");
          exit(1);
       }
       
       return q.first->data;
    }

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    const queue queueEmpty = { NULL, NULL };
    Should be just

    Code:
    const queue queueEmpty;
    If you want to initialise them, then dereference it and then initialise.

    ssharish

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Should be just
    Why?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But this is actually a variable definition:
    Code:
    const queue queueEmpty = { NULL, NULL };
    I would like to believe it causes problems, const or not.
    Do not define variable inside headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM