gpt4 book ai didi

c++ - 在不更改 makefile 后,Makefile 项目将不会构建(几乎相同的 make,工作正常)

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:15 26 4
gpt4 key购买 nike

我在linux下编译了一个makefile工程,就在昨天。现在项目无法编译,我不记得对 makefile 本身做了任何更改。它抛出一个 make: *** No rule to make target 'obj/TranquilMain.o', needed by 'tranquil;.停止。 make 错误。我所做的唯一真正的改变是,当我从另一个项目复制代码和 makefile 时,我更改了某些文件(包括依赖项)的名称,然后更改了 makefile 中的 _DEPS。必要位置的所有文件。

应该注意的是,如果我从 _OBJ 列表中删除除 TranquilMain.o 之外的所有其他文件,它可以正常编译。 enter code here我希望我能提供的不仅仅是代码,还有这些知识,但我不知道问题出在哪里。

适当的 Makefile:“makefile”

#!/usr/bin/make

CC = gcc
CP = g++

SRC_DIR = #.
OBJ_DIR = obj
INC_DIR = ../include
LIB_DIR = ../lib

LIBS = -lm -lSDL -lSDLmain -lSDL_image -lSDL_mixer -lSDL_ttf -lSDL_net -lGL -lGLU -lGLEW
CFLAGS = -I$(INC_DIR) -L$(LIB_DIR) -std=gnu++0x


_DEPS = DefaultConfig.h BaseApplication.h BasePlugin.h SDLImage.h SDLFont.h SDLWindow.h SDLInput.h SDLRenderer.h SDLApplication.h Math2D.h SDLTimer.h
DEPS = $(patsubst %,$(INC_DIR)/%,$(_DEPS))

_OBJ = TranquilMain.o BaseApplication.o BasePlugin.o SDLImage.o SDLFont.o SDLWindow.o SDLInput.o SDLRenderer.o SDLApplication.o Math2D.o SDLTimer.o
OBJ = $(patsubst %,$(OBJ_DIR)/%,$(_OBJ))


$(OBJ_DIR)/%.o: %.cpp $(DEPS)
$(CP) -c -o $@ $< $(CFLAGS)

tranquil: $(OBJ)
$(CP) -o ../bin/$@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean
clean:
rm -f $(OBJ_DIR)/*.o *~ core $(INC_DIR)/*~

显然无用的 TranquilMain.cpp(只是依赖项中的第一个文件)

#include <iostream>
#include <fstream>
#include <string>


int main( int argc, char* args[] )
{
bool running = true;

while( running == true )
{
}

return 0;
}

最佳答案

当我开始使用上面的Makefile,以及项目结构

├── include/
└── src/
├── Makefile
├── TranquilMain.cpp
└── obj/

然后我得到相同的 make: *** No rule to make target `obj/TranquilMain.o', needed by `tranquil'. 您在上面报告的错误。是什么原因造成的?让我们在 Debug模式下运行 make 来找出答案。

先添加

.SUFFIXES:
%:: SCCS/s.%
%:: RCS/%
%:: RCS/%,v
%:: %,v
%:: s.%

到 Makefile 的顶部以取消一些只会使调试输出困惑的默认规则。

然后,运行make -d:

GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0
Reading makefiles...
Reading makefile `Makefile'...
Updating makefiles....
Considering target file `Makefile'.
Looking for an implicit rule for `Makefile'.
No implicit rule found for `Makefile'.
Finished prerequisites of target file `Makefile'.
No need to remake target `Makefile'.
Updating goal targets....
Considering target file `tranquil'.
File `tranquil' does not exist.
Considering target file `obj/TranquilMain.o'.
File `obj/TranquilMain.o' does not exist.
Looking for an implicit rule for `obj/TranquilMain.o'.
Trying pattern rule with stem `TranquilMain'.
Trying implicit prerequisite `TranquilMain.cpp'.
Trying rule prerequisite `../include/DefaultConfig.h'.
Trying pattern rule with stem `TranquilMain'.
Trying implicit prerequisite `TranquilMain.cpp'.
Trying rule prerequisite `../include/DefaultConfig.h'.
Looking for a rule with intermediate file `../include/DefaultConfig.h'.
Avoiding implicit rule recursion.
No implicit rule found for `obj/TranquilMain.o'.
Finished prerequisites of target file `obj/TranquilMain.o'.
Must remake target `obj/TranquilMain.o'.
make: *** No rule to make target `obj/TranquilMain.o', needed by `tranquil'. Stop.

make 首先确保 Makefile 是最新的。然后它尝试通过执行深度优先搜索来构建所有必需的依赖项,从而使 tranquil 目标成为可能。 tranquil依赖于${OBJ},第一个元素是obj/TranquilMain.o。根据 ${OBJDIR}/%.o 规则,这取决于 TranquilMain.cpp 和所有 ${DEPS}${DEPS} 的第一个元素是 ../include/DefaultConfig.h,因此 make 会尝试构建它。但它不存在,也没有 build 它的规则。 make 得出结论,它无法使用此规则构建 obj/TranquilMain.o,因为缺少依赖项。它试图找到另一个规则来使用其他确实存在的依赖项来构建它,但是没有这样的规则。所以 make 停止,说,“没有 [有效] 规则 [存在或可以构建的依赖项] 来生成目标 obj/TranquilMain.o。”

解决方案是什么?确保所有依赖项都存在。使用此 Makefile,要编译任何内容,您的项目必须至少包含:

.
├── include/
│   ├── BaseApplication.h
│   ├── BasePlugin.h
│   ├── DefaultConfig.h
│   ├── Math2D.h
│   ├── SDLApplication.h
│   ├── SDLFont.h
│   ├── SDLImage.h
│   ├── SDLInput.h
│   ├── SDLRenderer.h
│   ├── SDLTimer.h
│   └── SDLWindow.h
├── lib/
└── src/
├── BaseApplication.cpp
├── BasePlugin.cpp
├── Makefile
├── Math2D.cpp
├── SDLApplication.cpp
├── SDLFont.cpp
├── SDLImage.cpp
├── SDLInput.cpp
├── SDLRenderer.cpp
├── SDLTimer.cpp
├── SDLWindow.cpp
├── TranquilMain.cpp
└── obj/

而且您可能需要将库文件添加到 lib/ 以获得最终的可执行文件。

关于c++ - 在不更改 makefile 后,Makefile 项目将不会构建(几乎相同的 make,工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com