0

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
27
  • Because gcc -c is compiling only without linking. Giving it *.o files is useless as the warning is friendly suggesting. Commented Dec 20, 2016 at 22:02
  • -c means compile only (don't link). So a .o file cannot be provided as it cannot be compiled (it's already compiled and needs to be linked). Commented Dec 20, 2016 at 22:02
  • 1
    Also note that <stdbool.h> from C99 provides bool, true, false, so you don't need the definition typedef enum { false, true } bool;, though it does no great harm. None of the four headers that are included in stack.h are actually needed by stack.h; you should not clutter your headers with unnecessary other headers. The Op typedef is needed; the typedef struct List List; should be unconditional (the consumers of your abstraction shouldn't need to care how your list type is implemented; that is purely an implementation concern). You should radically simplify your header. Commented Dec 20, 2016 at 22:31
  • 2
    Tons of useless code, when the reason was an incomplete command line. Commented Dec 20, 2016 at 22:32
  • 1
    the file: stackimpl.c seems to be missing the statement: #include "stack.h" Strongly suggest a header file for a *.c file have the same name as the *.c file with the only difference being the name extension is .h rather than .c Commented Dec 21, 2016 at 12:22

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

11 Comments

But linker accepts arrayImpl.o files for linking right?
seems not, why not just put arrayImpl.c
I did use linker to link main.c with multiple .o files in the past. If gcc actually run ld after preprocessing(cpp), compilation(cc) & assembling(as), Does gcc take .o or .c to link? This is why I gave ../list/arrayImpl.o
@overexchange It's quite unusual to compile more than 1 source code file at a time, If you do it the traditional way and compile stackImpl.c also , and then do gcc -DARRAY main.c stackImpl.o ../list/arrayImpl.o it will work.
Please add that info to question. It's wrong and is the cause of the problem. Missing -DARRAY.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.