Frage

This is the first time I write a Makefile. I tried the following Makefile to create object file for each source file in src folder, merge them and create the binary file out.exe in the bin folder.

SRC=src
INC=inc
OBJ=obj
BIN=bin

CC=/usr/sfw/bin/gcc
FLAGS=-g -m64 -Wall

ALL_INC=-I$(INC)

CC_LIBS=-lsocket -lnsl -lpthread -lm -lstdc++ -lclntsh

$(BIN)/out.exe : $(OBJ)/%.o
   $(CC) $(ALL_INC) $(FLAGS) -o out.exe

$(OBJ)/%.o : $(SRC)/%.cpp $(INC)/%.h
   $(CC) $(ALL_INC) $(FLAGS) -c $<

clean:
   rm -f obj/*.o bin/ussd

On invoking make I am getting the following error.

make: Fatal error: Don't know how to make target `obj/%.o'

Below I have given the list of make tools available in my system and their version

/bin - dmake - Sun Distributed Make 7.7
/usr/bin - dmake - Sun Distributed Make 7.7
/usr/ccs/bin - make - Unknow version
/usr/sfw/bin - gmake - GNU Make 3.80
/usr/xpg4/bin - Unknow version
/usr/local/bin - make - GNU Make version 3.79.1
War es hilfreich?

Lösung

The problem is in this line:

$(BIN)/out.exe : $(OBJ)/%.o
   $(CC) $(ALL_INC) $(FLAGS) -o out.exe

$(BIN)/out.exe must know all files it depends on

Add this:

SRCS=$(wildcard $(SRC)/*.cpp) // List all source files
OBJS=$(SRCS:$(SRC)/%.cpp=$(OBJ)/%.o) // List all object files (one for each source file)

Then change the out.exe rule:

$(BIN)/out.exe : $(OBJS)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top