Sam Roy
Sam Roy

Reputation: 33

Error while referencing a header file from a different directory

My directory structure is as follows:

enter image description here

My diskdriver.c which is in disk

#include <stdio.h>
#include <stdlib.h>
#include "diskdriver.h"

const int BLOCK_SIZE = 512;
const int NUM_BLOCKS = 4096;

//Reads one block at a time from disk
void readBlock(FILE* disk, int blockNum, char* buffer){
  fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
  fread(buffer, BLOCK_SIZE, 1, disk);
}

//Writes one block at a time to disk
void writeBlock(FILE* disk, int blockNum, char* data){
  fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
  fwrite(data, BLOCK_SIZE, 1, disk);
}

My diskdriver.h header file is:


#ifndef DISKDRIVER_H
#define DISKDRIVER_H

//Reads one block at a time from disk
void readBlock(FILE* disk, int blockNum, char* buffer);

//Writes one block at a time to disk
void writeBlock(FILE* disk, int blockNum, char* data);

#endif /* DISKDRIVER_H */

I am accessing it from file.c which is in io

My makefile looks like:

CC:=gcc
CFLAGS:=-g -Wall -Werror

TESTFILES := $(wildcard apps/test*.c)
$(info TESTFILES are $(TESTFILES))

TESTS := $(TESTFILES:apps/%.c=%)
$(info TESTS are $(TESTS))

all: $(TESTS) disk.o file.o

test%: apps/test%.c file.o
    $(CC) $(CFLAGS) -o apps/$@ $^

disk.o: disk/diskdriver.c disk/diskdriver.h
    $(CC) $(CFLAGS) -c -o $@ $<

file.o: io/File.c io/File.h
    $(CC) $(CFLAGS) -c -o $@ $<

.PHONY: clean

clean:
    rm -rf *.o
    find apps -type f -not -name '*.c' -print0 | xargs -0 rm --

however I get the error:

make all
TESTFILES are apps/test01.c
TESTS are test01
gcc -g -Wall -Werror -c -o file.o io/File.c
gcc -g -Wall -Werror -o apps/test01 apps/test01.c file.o
file.o: In function `initLLFS':
/home/samroy2106/Desktop/CSC360/A3/file_system/io/File.c:34: undefined reference to `writeBlock'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'test01' failed
make: *** [test01] Error 1

I'm pretty sure that it's a small error but I can't seem to put a finger on it. Thank you so much in advance :)

Upvotes: 3

Views: 47

Answers (1)

Ashwani
Ashwani

Reputation: 2052

I can see cc -g -Wall -Werror -o apps/test01 apps/test01.c file.o is missing disk.o which is where writeBlock is defined.
Add disk.o to the prerequisites of test

test%: apps/test%.c file.o disk.o
    $(CC) $(CFLAGS) -o apps/$@ $^

Upvotes: 2

Related Questions