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
gcc -cis compiling only without linking. Giving it*.ofiles is useless as the warning is friendly suggesting.-cmeans compile only (don't link). So a.ofile cannot be provided as it cannot be compiled (it's already compiled and needs to be linked).<stdbool.h>from C99 providesbool,true,false, so you don't need the definitiontypedef enum { false, true } bool;, though it does no great harm. None of the four headers that are included instack.hare actually needed bystack.h; you should not clutter your headers with unnecessary other headers. TheOptypedef is needed; thetypedef 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.stackimpl.cseems 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.hrather than.c