简体   繁体   English

make中隐含规则的问题

[英]Problems with implicit rule in make

I have the following directory structure 我有以下目录结构

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

I would like to use an implicit rule to compile all the .cc files in root\\src to .o files in root\\obj . 我想使用隐式规则将root\\src所有.cc文件编译为root\\obj .o文件。

This is my makefile so far: 到目前为止,这是我的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}

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

Any idea what's going wrong? 知道发生了什么事吗? I'm pretty new to makefiles so this might be terribly obvious. 我对makefile非常陌生,因此这可能非常明显。

Applying all of the ideas from the comments (and adding a couple trivial ones of my own), we get: 应用评论中的所有想法(并添加一些我自己的琐碎想法),我们得到:

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}

What does "make Cyborg" do with this Makefile? “ make Cyborg”与此Makefile有什么关系?

This is probably going to take a few iterations. 这可能要花一些迭代。

Try some simple rules, in order of increasing complexity, and tell us the results: 尝试一些简单的规则,以增加复杂性的顺序,然后告诉我们结果:

./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 $<
${obj}\%.o : ${src}\%.c --> ${obj}\%.o : ${src}\%.cc

I had similar problem with GNU make. 我对GNU make也有类似的问题。 Try making explisit rules for your objects: 尝试为您的对象制定显式规则:

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

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

# and so on for each object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM