user8667363
user8667363

Reputation: 21

Error "no such file or directory" when compiling with Makefile

I am trying to use a Makefile to compile a couple of .c files and a couple of custom .h files to make a single executable file. however during the compilation im receiving the error that the error "No such file or directory".

Here is my Makefile, is there a possible flaw in the logic as to why the header files are being forgotten during compilation?

The error occurs on line 21 of the compilation during the $(BIN)main.o rule.

This is the compilation time error:

socs@socsvm:~/Desktop/Programs/CIS2520/root$ make
gcc -Wall -g -std=c99 -c -Iinclude -c ./src/main.c
./src/main.c:4:23: fatal error: structDefns: No such file or directory
#include "structDefns"
                      ^
compilation terminated.
makefile:21: recipe for target 'bin/main.o' failed
make: *** [bin/main.o] Error 1
socs@socsvm:~/Desktop/Programs/CIS2520/root$ 

This is the makefile:

CC = gcc
CFLAGS = -Wall -g -std=c99 -Iinclude

BIN = ./bin/
SRC = ./src/
INC = ./include/

$(BIN)main:     $(BIN)main.o $(BIN)book.o $(BIN)store.o $(BIN)boardGame.o
        $(CC) -o $(BIN)main $(BIN)main.o $(BIN)book.o $(BIN)store.o 
$(BIN)boardGame.o

$(BIN)book.o:   $(SRC)book.c $(INC)structDefns.h $(INC)funcDefns.h
        $(CC) $(CFLAGS) -c $(SRC)book.c

$(BIN)boardGame.o:      $(SRC)boardGame.c $(INC)structDefns.h $(INC)funcDefns.h
        $(CC) $(CFLAGS) -c $(SRC)boardGame.c

$(BIN)store.o:  $(SRC)store.c $(INC)structDefns.h $(INC)funcDefns.h
        $(CC) $(CFLAGS) -c $(SRC)store.c

$(BIN)main.o:   $(SRC)main.c $(INC)structDefns.h $(INC)funcDefns.h
                $(CC) $(CFLAGS) -c $(SRC)main.c

As for the file directory, the make file is outside of 3 folders, bin, src, and include. bin is where I want the object and executable to go, src is where the .c files live, and the .h files live in the include folder.

Upvotes: 1

Views: 52102

Answers (1)

user707650
user707650

Reputation:

From the compilation command, the error and the source line given by the error:

gcc -Wall -g -std=c99 -c ./src/main.c
./src/main.c:4:23: fatal error: structDefns: No such file or directory
#include "structDefns"

it shows that gcc can't find the include file. It expects it in the same directory as main, but your Makefile shows it lives in ./include (or even ../include relative to main).

Use the -I flag for gcc together with the proper path. You can set this alongside the other CFLAGS:

CFLAGS = -Wall -g -std=c99 -Iinclude

should do it. (I may be slightly mistaken with the path. If it still fails, try variations of -I./include or the relative path to main: -I../include. But it should be the path from the directory where you issue the command, not relative to main.c.)

Upvotes: 2

Related Questions