Home > Mobile >  Makefile cant find directories C
Makefile cant find directories C

Time:09-25

I am working on a project related to Chess and I use a makefile to compile everything. My goal is to compile only files that have been changed,instead of everything everytime. Folder structure can be seen clearer here. make main.o results in an error :

fatal error: chesspieces.h: No such file or directory
    5 | #include "chesspieces.h"

And pretty much everything else gives me a No rule to make target .o . Stop. I am using make from chess/src and I have every folder included in my Includes variable. Make file here:

CC = g  
INCLUDES =-Icomponents -IBoard -Ipieces -IApplication
OUTPUT = -o Chess

Chess: main.o board.o player.o chess.o
    $(CC) main.o board.o player.o chess.o $(OUTPUT)

main.o: main.cc board.h chess.h player.h chesspieces.h
    $(CC) $(INCLUDES) -c main.cc

position.o: position.cc position.h
    $(CC) $(INCLUDES) -c position.cc
    
piece.o: piece.cc piece.h color.h position.h
    $(CC) $(INCLUDES) -c piece.cc

rook.o: rook.cc rook.h piece.h
    $(CC) $(INCLUDES) -c rook.cc

queen.o: queen.cc queen.h piece.h
    $(CC) $(INCLUDES) -c queen.cc

pawn.o: pawn.cc pawn.h piece.h
    $(CC) $(INCLUDES) -c pawn.cc

knight.o: knight.cc knight.h piece.h
    $(CC) $(INCLUDES) -c knight.cc

king.o: king.cc king.h piece.h
    $(CC) $(INCLUDES) -c king.cc

bishop.o: bishop.cc bishop.h piece.h
    $(CC) $(INCLUDES) -c bishop.cc

square.o: square.cc square.h color.h position.h
    $(CC) $(INCLUDES) -c square.cc

board.o: board.cc board.h square.h position.h
    $(CC) $(INCLUDES) -c board.cc

player.o: player.cpp player.h board.h piece.h
    $(CC) $(INCLUDES) -c player.cc

chess.o: chess.cc chess.h player.h board.h piece.h
    $(CC) $(INCLUDES) -c chess.cc

Making the dependencies my thinkings was to add every header included in the file,not sure about that

EDIT: Compiling on command line works fine(Besides the linking errors) g -Icomponents -IBoard -IPieces -Iapplication main.cc So something is wrong with the $(INCLUDES) variable,can't figure out why but it's blank: g -c -o main.o main.cc main.cc:5:10: fatal error: chesspieces.h: No such file or directory

Folders1 Folders2

CodePudding user response:

The thing you didn't make clear in your question is the structure of your directory, or the directory you were in when you ran make. By reviewing the github repo we can see that your makefile is in the root directory and your source files are in subdirectories: for example you have src/main.cc, src/Application/chess.cc, etc.

But in your makefile, you don't provide any paths. Your rules are:

main.o: main.cc board.h chess.h player.h chesspieces.h
        $(CC) $(INCLUDES) -c main.cc

These files you've mentioned here don't exist, so make will fail. I'm actually confused about how you're getting the behavior you are: you should get errors like, No rule to build main.cc or something like that.

Maybe you're not running make in the root directory of your source tree, but rather somewhere else? In that case make can't find your makefile and it's just using default rules, which explains why your INCLUDES variable is not found.

CodePudding user response:

The variable 'INCLUDES' of the 'Makefile' file contains command line switches that tell the compiler in which directory to look for include files. The '-Ipieces' key tells the compiler to look for include files in the subdirectory named 'pieces' of the current directory. The file 'chesspieces.h' should be in one of the subdirecties that enumerated in variable 'INCLUDES'.

  • Related