confusion on linking files
Hello,
The following code just implements a dynamic array that is grown to double in size once its full. I do not understand why the reference cannot be resolved by the linker:
I have a file "memory.h" and "memory.c":
Code:
// memory.h
#ifndef clox_memory_h
#define clox_memory_h
#include "common.h"
#define GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
#define GROW_ARRAY(type, pointer, oldCount, newCount) \
(type*)reallocate(pointer, sizeof(type) * (oldCount), \
sizeof(type) * (newCount))
void* reallocate(void* pointer, size_t oldSize, size_t newSize);
#endif
Code:
// memory.c
#include <stdlib.h>
#include "memory.h"
void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(pointer);
return NULL;
}
void* result = realloc(pointer, newSize);
if (result == NULL){
exit(1);
}
return result;
}
and the file that includes "memory.h" - "segment.c":
Code:
// segment.c where the linker cannot resolve reallocate()
// in append_segment()
#include "segment.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "memory.h"
void init_segment(code_segment* segment)...
// the error originates here...
void append_segment(code_segment* segment, uint8_t byte){
if (segment->count + 1 > segment->capacity){
segment->code = GROW_ARRAY(uint8_t, segment->code,
segment->capacity, GROW_CAPACITY(segment>capacity));
}
segment->code[segment->count] = byte;
segment->count++;
}
void free_segment(code_segment* segment)...
this is the makefile and I dont understand why the linker cannot resolve
the reference to "reallocate()" in "append_segment()".
Code:
all: main test_segment segment.o
... (more targets)
segment.o: segment.c memory.c
gcc -Wall -c segment.c -o segment.o
memory.o: memory.c
gcc -Wall -c memory.c -o memory.o
... (more targets)