1
votes

I wrote a makefile to compile and link all the files in my project. Right now i only have 2 cpp files: src/main.cpp and src/DBEngine/benchmark/ssb/ssb_main.cpp.

My makefile content is :

CPP_FILES := $(wildcard src/*.cpp) $(wildcard src/DBEngine/benchmark/ssb/*.cpp) 
OBJ_FILES := $(addprefix bin/obj/,$(notdir $(CPP_FILES:.cpp=.o)))
DEBUG_OBJ_FILES := $(addprefix bin/debug/,$(notdir $(CPP_FILES:.cpp=.o)))
CC_FLAGS := -I${PWD}

main.out: $(OBJ_FILES)
    g++ -o $@ $(OBJ_FILES)

bin/obj/%.o: src/%.cpp
g++ -c -o $@ $< (CC_FLAGS) -w

But when i do a make it gives the error :

make: *** No rule to make target bin/debug/ssb_main.o, needed by main.out. Stop.

3

3 Answers

1
votes

Short version: your pattern rule is expanded as follows for the object files in DBEngine

bin/obj/ssb_main.o: src/ssb_main.cpp

You can see how that won't work I'm sure.

You can add another pattern rule to fix it

bin/obj/%.o: src/DBEngine/benchmark/ssb/%.cpp
    g++ -c -o $@ $< $(CC_FLAGS) -w

A simpler method however is to mirror your source and build trees and use vpath, make sure there is a directory called DBEngine/benchmark/ssb in the makefile directory as well, and use the following:

CPP_FILES := $(wildcard src/*.cpp) $(wildcard src/DBEngine/benchmark/ssb/*.cpp) 
OBJ_FILES := $(CPP_FILES:src/%.cpp=%.o)
CPPFLAGS  := -I${PWD}
CXXFLAGS  := -w

vpath %.cpp src

main: $(OBJ_FILES)

The built-in rules will handle the rest.

Side notes: -I${PWD} is superfluous, and -w is usually a bad idea.

1
votes

In the compiling rule you have specified that the source files are in the 'src' directory. But from your description it seems that the ssb_main.cpp file is in the 'src/DBEngine/benchmark/ssb/' folder, so make does not find the dependency to compile ssb_main.o.

You can use variable VPATH to specify the folders where make should look for your source files.

0
votes

Since you have sub-folder is your src folder you can try to use

CPP_FILES := $(shell ls -pR ./src | grep -v /)

It will list all cpp files under src and its sub-folders excluding . and .. .