Home > Back-end >  makefile, error compiling files from different paths
makefile, error compiling files from different paths

Time:05-11

I have 2 folders, the first folder contains the files I've built, and the second folder contains files I should be using without changing, all the files synchronize and work, already tested by manually compiling them while combined in 1 folder,

    CC:=g  
    COURSE_DIR:=/home/mtm/public/2122b/
    HW_DIR:=$(COURSE_DIR)ex2/
    OBJS=Card.o Player.o utilities.o Mtmchkin.o test.o
    EXEC=mtmchkin_test
    DEBUG_FLAG:=-g -DNDEBUG#assign -g for debug, or -DNDEBUG to turnoff assert
    HW_FLAG=-I -I$(HW_DIR)
    COMP_FLAG=--std=c  11 -Wall -pedantic-errors -Werror $(HW_FLAG)
    
    $(EXEC) : $(OBJS)
            $(CC) $(DEBUG_FLAG) $(COMP_FLAG) $(OBJS) -o $@
    Card.o : Card.cpp $(HW_DIR)Card.h Player.h $(HW_DIR)utilities.h
            $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.cpp -o $@
    Player.o : Player.cpp Player.h $(HW_DIR)utilities.h $(HW_DIR)Card.h
            $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.cpp -o $@
    utilities.o : $(HW_DIR)utilities.cpp $(HW_DIR)utilities.h
            $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.cpp -o $@
    Mtmchkin.o : Mtmchkin.cpp Mtmchkin.h $(HW_DIR)Card.h Player.h $(HW_DIR)utilities.h
            $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.cpp -o $@
    test.o : $(HW_DIR)test.cpp Player.h $(HW_DIR)Card.h Mtmchkin.h $(HW_DIR)utilities.h
            $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.cpp -o $@
    clean:
        rm -f $(OBJS) $(EXEC)

The makefile is in the first folder as I mentioned, and the HW_DIR is the path to the 2nd folder I mentioned, but it doesn't compile. Any idea on what the problem is and how I can fix it?

Edit: This is the input I've made from the first folder i mentioned and contains the makefile and output

    [final]$ make
    g   -c -g -DNDEBUG --std=c  11 -Wall -pedantic-errors -Werror -I -I/home/mtm/public/2122b/ex2/ Card.cpp -o Card.o
    Card.cpp:1:18: fatal error: Card.h: No such file or directory
     #include "Card.h"
                      ^
    compilation terminated.
    make: *** [Card.o] Error 1

I want to it to compile successfully and put the exec file in the same folder as the makefile.

CodePudding user response:

Your problem appears to be:

HW_FLAG=-I -I$(HW_DIR)

This sets the include path to -I/home/mtm/public/2122b/ex2/ which (as a relative path, since it begins with -) would be a (presumably nonexistent) directory under the current directory.

it should be:

HW_FLAG=-I $(HW_DIR)

GCC's command line documentation shows that -I can have a space before its argument.

Also, make sure that all of the different folders where your include files live are represented in the include path. If (as it appears) there are .h files in the same folder as the makefile, you can try

 -I . -I $(HW_DIR)

However, you may need to specify the full path to the makefile instead of .

Finally, $* won't work for compiling $(HW_DIR)utilities.cpp and $(HW_DIR)test.cpp because they're in a different directory from the targets utilities.o and test.o. (documentation link). Just use e.g. $(HW_DIR)\utilities.cpp explicitly for that rule, instead of $*.cpp.

  • Related