overexchange
overexchange

Reputation: 1

Linker error - undefined reference -gcc

After running,

 Cygwin-PC ~/code_practice/Computing/list
  $ gcc -c arrayImpl.c -o arrayImpl.o

Code directory structure, in windows(cygwin)

Cygwin-PC ~/code_practice/Computing/stack
$ ls
main.c  stack.h  stackImpl.c

Cygwin-PC ~/code_practice/Computing/list
$ ls
arrayImpl.c  arrayImpl.o  linkedListImpl.c  list.h  main.c

Stack abstraction depends on List abstraction, for data representation & usage.

List abstraction is as shown below,

/************ list.h ************/


/***************** Usage-start ************/
typedef enum{false, true}bool;
typedef enum {CREATE_NEW_LIST, DOUBLE_THE_LIST, HALF_THE_LIST}Op;

#if defined(ARRAY)

  /* To ensure Encapsulation(i.e., maintain invariants of array) */
  typedef struct List List;

#elif defined(LINKED_LIST)

  /* To ensure Encapsulation(i.e., maintain invariants of linked list) */
  /* User will not get access to node*/
  typedef struct List List;


#else
  #error "Wrong list implementation macro name !!!"
#endif


void insertItem(List *, void *newItem);
void *deleteItem(List *, int listIndex);
void *deleteLastItem(List *);


List* createList(List *, Op opType);

/***************** Usage-end ***************/

/***************** arrayImple.c **************/

#if defined(ARRAY)

#include"list.h"


/************ Representation - start ************/
typedef struct List{
  void **array;

  /* Following members for Housekeeping - Array enhancement*/
  int lastItemPosition;
  int size;
}List;

#define INITIAL_LIST_SIZE 50
/********************* Representation - end ************/




/************* Usage - start ***************/
List *createList(List *list, Op opType){

       ....
}

void insertItem(List *arrayList, void *newItem){
            ...
}

void *deleteItem(List *arrayList, int listIndex){
     ....
}

void * deleteLastItem(List *arrayList){
     ...
}
/******************** Usage - end *******************/

#endif

Stack abstraction, is shown below,

/********* stack.h *********/

#include"../list/list.h"

typedef struct Stack Stack;

Stack *createStack();
void push(Stack *, void *item);
void*pop(Stack *);

/*********** stackImpl.c *******/

#include"../list/list.h"

typedef struct Stack{
  List *stack;
}Stack;

Stack* createStack(){

  Stack *s = malloc(sizeof(Stack));
  s->stack = createList((void *)0, CREATE_NEW_LIST);

  return s;
}

void push(Stack *s, void *item){
  insertItem(s->stack, item);
}

void *pop(Stack *s){
  void *item = deleteLastItem(s->stack);
  return item;
}

Below compilation says, with given below message, which does not involve linker,

Cygwin-PC ~/code_practice/Computing/stack
   $ gcc -c -DARRAY main.c stackImpl.c ../list/arrayImpl.o
gcc: warning: ../list/arrayImpl.o: linker input file unused because linking  
      not done

Below compilation fails, with given below error,

Cygwin-PC ~/code_practice/Computing/stack
   $ gcc -DARRAY main.c stackImpl.c ../list/arrayImpl.o
/tmp/ccapgYQI.o:stackImpl.c:(.text+0x25): undefined reference to 
   `createList'
/tmp/ccapgYQI.o:stackImpl.c:(.text+0x4b): undefined reference to 
   `insertItem'
/tmp/ccapgYQI.o:stackImpl.c:(.text+0x61): undefined reference to 
   `deleteLastItem'
collect2: error: ld returned 1 exit status

Question,

From above compilation command, Why GNU linker(ld) does not accept ../list/arrayImpl.o file to find the definitions of createList(), insertItem() & deleteLastItem()

Below compilation works,

  Cygwin-PC ~/code_practice/Computing/stack
    $  gcc -DARRAY main.c stackImpl.c ../list/arrayImpl.c

Upvotes: 0

Views: 1033

Answers (2)

pm100
pm100

Reputation: 50100

this

Cygwin-PC ~/code_practice/Computing/stack
   $ gcc -DARRAY main.c stackImpl.c ../list/arrayImpl.o
/tmp/ccapgYQI.o:stackImpl.c:(.text+0x25): undefined reference to 
   `createList'
/tmp/ccapgYQI.o:stackImpl.c:(.text+0x4b): undefined reference to 

failed becuase you missed off arrayImpl.c

kaylum has the right answer, when you compiled arrayImpl.c you forgot -DARRAY

Upvotes: 0

kaylum
kaylum

Reputation: 14046

gcc -c arrayImpl.c -o arrayImpl.o

That is missing -DARRAY. Without that the code in arrayImpl.c is conditionally removed:

/***************** arrayImple.c **************/

#if defined(ARRAY)

Upvotes: 1

Related Questions