make中隐式规则的问题

时间:2012-02-22 01:11:45

标签: c++ makefile

我有以下目录结构

         (root)
  /      /      \        \
/       |       |         \
src    obj  include     bin

我想使用隐式规则来编译.ccroot\src.o个文件中的所有root\obj个文件。

到目前为止,这是我的makefile:

basedir = .
incl = ${basedir}\include
obj = ${basedir}\obj
src = ${basedir}\src
lib = ${basedir}\lib
bin = ${basedir}\bin

CXX = gcc

LDLIBS = -lstdc++ -lmingw32 -lSDLmain -lSDL -lSDL_image -lchipmunk -lSDL_ttf \
-lSDL_mixer

LDFLAGS = -L${lib}

objects = $(addprefix ${obj}\, GameObject.o PhysicalObject.o \
Terrain.o Timer.o main.o ImageLoader.o Player.o )

sources = $(addprefix ${src}\, GameObject.cc PhysicalObject.cc \
Terrain.cc Timer.cc main.cc ImageLoader.cc Player.cc )

Cyborg : ${objects}
    ${CXX} ${objects} -o ${bin}\Cyborg.exe -L${lib} ${LDFLAGS} ${LDFLAGS}

${obj}\%.o : ${src}\%.c
    ${CXX} ${src}\$^ -c -o ${obj}\$@ 

.PHONY: clean

clean :
    rm ${bin}\Cyborg.exe ${objects}

我得到的错误是make: *** No rule to make target .\obj\GameObject.o, needed by Cyborg. Stop.

知道出了什么问题吗?我很擅长制作文件,所以这可能非常明显。

4 个答案:

答案 0 :(得分:3)

应用评论中的所有想法(并添加我自己的一些简单的想法),我们得到:

basedir = .
incl = ${basedir}/include
obj = ${basedir}/obj
src = ${basedir}/src
lib = ${basedir}/lib
bin = ${basedir}/bin

CXX = g++

LDLIBS = -lstdc++ -lmingw32 -lSDLmain -lSDL -lSDL_image -lchipmunk -lSDL_ttf \
-lSDL_mixer

LDFLAGS = -L${lib}

objects = $(addprefix ${obj}/, GameObject.o PhysicalObject.o \
Terrain.o Timer.o main.o ImageLoader.o Player.o )

sources = $(addprefix ${src}/, GameObject.cc PhysicalObject.cc \
Terrain.cc Timer.cc main.cc ImageLoader.cc Player.cc )

Cyborg : ${objects}
    ${CXX} ${objects} -o ${bin}/Cyborg.exe -L${lib} ${LDFLAGS} ${LDFLAGS}

${obj}/%.o : ${src}/%.c
    ${CXX} $^ -c -o $@ 

.PHONY: clean Cyborg

clean :
    rm -f ${bin}\Cyborg.exe ${objects}

“make Cyborg”对这个Makefile做了什么?

答案 1 :(得分:1)

这可能需要几次迭代。

尝试一些简单的规则,以增加复杂性,并告诉我们结果:

./obj/GameObject.o : ./src/GameObject.cc
    @echo trying to build $@ from $<

./obj/GameObject.o : ./obj/%.o : ./src/%.cc
    @echo trying to build $@ from $<

$(objects) : ./obj/%.o : ./src/%.cc
    @echo trying to build $@ from $<

./obj/%.o : ./src/%.cc
    @echo trying to build $@ from $<

答案 2 :(得分:0)

${obj}\%.o : ${src}\%.c --> ${obj}\%.o : ${src}\%.cc

答案 3 :(得分:-2)

我和GNU make有类似的问题。尝试为对象制定明确的规则:

$(obj)\GameObject.o: $(src)\GameObject.cc

$(obj)\PhysicalObject.o: $(src)\PhysicalObject.cc

# and so on for each object