查找带有 c++ 的标头时出现问题生成文件
我已经开始使用我的第一个 makefile。我正在使用 libtcod 库用 C++ 编写一个 roguelike,并使用以下 hello world 程序来测试我的环境是否已启动并运行:
#include "libtcod.hpp"
int main()
{
TCODConsole::initRoot(80, 50, "PartyHack");
TCODConsole::root->printCenter(40, 25, TCOD_BKGND_NONE, "Hello World");
TCODConsole::flush();
TCODConsole::waitForKeypress(true);
}
我的项目目录结构如下所示:(
/CppPartyHack
----/libtcod-1.5.1 # this is the libtcod root folder
--------/include
------------libtcod.hpp
----/PartyHack
--------makefile
--------partyhack.cpp # the above code
当我们在这里时,我如何进行正确的缩进?使用这些破折号是愚蠢的。)
这是我的 makefile:
SRCDIR = .
INCDIR = ../libtcod-1.5.1/include
CFLAGS = $(FLAGS) -I$(INCDIR) -I$(SRCDIR) -Wall
CC = gcc
CPP = g++
.SUFFIXES: .o .h .c .hpp .cpp
$(TEMP)/%.o : $(SRCDIR)/%.cpp
$(CPP) $(CFLAGS) -o $@ -c $<
$(TEMP)/%.o : $(SRCDIR)/%.c
$(CC) $(CFLAGS) -o $@ -c $<
CPP_OBJS = $(TEMP)partyhack.o
所有 : partyhack
partyhack : $(CPP_OBJS)
$(CPP) $(CPP_OBJS) -o $@ -L../libtcod-1.5.1 -ltcod -ltcod++ -Wl,-rpath,.
干净的 :
\rm -f $(CPP_OBJS) partyhack
我正在使用 Ubuntu,我的终端出现以下错误:
max@max-desktop:~/Desktop/Development/CppPartyhack/PartyHack$ make
g++ -c -o partyhack.o partyhack.cpp
partyhack.cpp:1:23: 错误: libtcod.hpp: 没有这样的文件或目录
partyhack.cpp:在函数“int main()”中:
partyhack.cpp:5:错误:“TCODConsole”尚未声明
partyhack.cpp:6:错误:“TCODConsole”尚未声明
partyhack.cpp:6:错误:“TCOD_BKGND_NONE”未在此范围内声明
partyhack.cpp:7:错误:“TCODConsole”尚未声明
partyhack.cpp:8:错误:“TCODConsole”尚未声明
make: *** [partyhack.o] Error 1
显然,makefile 找不到 libtcod.hpp。我已经仔细检查过,并且确信 INCDIR 中 libtcod.hpp 的相对路径是正确的,但由于我刚刚开始使用 makefile,我不确定还有什么可能是错误的。我的 makefile 基于 libtcod 设计者与库本身一起提供的模板,虽然我查看了一些在线 makefile 教程,但此 makefile 中的代码比教程中显示的任何示例都要复杂一些,所以我假设我在转换中搞砸了一些基本的东西。感谢您的任何帮助。
I've started working with my first makefile. I'm writing a roguelike in C++ using the libtcod library, and have the following hello world program to test if my environment's up and running:
#include "libtcod.hpp"
int main()
{
TCODConsole::initRoot(80, 50, "PartyHack");
TCODConsole::root->printCenter(40, 25, TCOD_BKGND_NONE, "Hello World");
TCODConsole::flush();
TCODConsole::waitForKeypress(true);
}
My project directory structure looks like this:
/CppPartyHack
----/libtcod-1.5.1 # this is the libtcod root folder
--------/include
------------libtcod.hpp
----/PartyHack
--------makefile
--------partyhack.cpp # the above code
(while we're here, how do I do proper indentation? Using those dashes is silly.)
and here's my makefile:
SRCDIR = .
INCDIR = ../libtcod-1.5.1/include
CFLAGS = $(FLAGS) -I$(INCDIR) -I$(SRCDIR) -Wall
CC = gcc
CPP = g++
.SUFFIXES: .o .h .c .hpp .cpp
$(TEMP)/%.o : $(SRCDIR)/%.cpp $(CPP) $(CFLAGS) -o $@ -c
lt; $(TEMP)/%.o : $(SRCDIR)/%.c $(CC) $(CFLAGS) -o $@ -clt;CPP_OBJS = $(TEMP)partyhack.o
all : partyhack
partyhack : $(CPP_OBJS) $(CPP) $(CPP_OBJS) -o $@ -L../libtcod-1.5.1 -ltcod -ltcod++ -Wl,-rpath,.
clean : \rm -f $(CPP_OBJS) partyhack
I'm using Ubuntu, and my terminal gives me the following errors:
max@max-desktop:~/Desktop/Development/CppPartyhack/PartyHack$ make
g++ -c -o partyhack.o partyhack.cpp
partyhack.cpp:1:23: error: libtcod.hpp: No such file or directory
partyhack.cpp: In function ‘int main()’:
partyhack.cpp:5: error: ‘TCODConsole’ has not been declared
partyhack.cpp:6: error: ‘TCODConsole’ has not been declared
partyhack.cpp:6: error: ‘TCOD_BKGND_NONE’ was not declared in this scope
partyhack.cpp:7: error: ‘TCODConsole’ has not been declared
partyhack.cpp:8: error: ‘TCODConsole’ has not been declared
make: *** [partyhack.o] Error 1
So obviously, the makefile can't find libtcod.hpp. I've double checked and I'm sure the relative path to libtcod.hpp in INCDIR is correct, but as I'm just starting out with makefiles, I'm uncertain what else could be wrong. My makefile is based off a template that the libtcod designers provided along with the library itself, and while I've looked at a few online makefile tutorials, the code in this makefile is a good bit more complicated than any of the examples the tutorials showed, so I'm assuming I screwed up something basic in the conversion. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里发生的情况是,
1) make 评估目标
all
,它解析为partyhack
。2) make 计算目标
partyhack
,解析为$(CPP_OBJS)
3) make 计算目标
$(CPP_OBJS)
,解析为$(TMP)partyhack.o
4) make 计算解析为
partyhack.o
的目标$(TMP)partyhack.o
这是因为TMP 未定义。另请注意,
$(TMP)
后面缺少斜杠。5) make 评估目标
partyhack.o
,并应用隐式规则g++ -c -o partyhack.o partyhack.cpp
它不应用您指定的规则,即
$(TEMP)/%.o : $(SRCDIR)/%.cpp
因为 TEMP 未定义,所以计算结果为/partyhack.o : ./partyhack.cpp
,并且我们没有构建/partyhack.o
因为我们不在根目录中。6) g++ 找不到包含文件,因为包含目录没有传递给它,因为你的规则没有应用。
要解决此问题:
首先,您需要定义 TEMP(请参阅 Nick Meyers 的回答)。
其次,您需要更改 CPP_OBJS 的定义(如 Paul R 建议的那样)。
如果您在目录 CppPartyHack/PartyHack 中调用 make,这将起作用。
What happens here, is that
1) make evaluates the target
all
, which resolves topartyhack
.2) make evaluates the target
partyhack
, which resolves to$(CPP_OBJS)
3) make evaluates the target
$(CPP_OBJS)
, which resolves to$(TMP)partyhack.o
4) make evaluates the target
$(TMP)partyhack.o
which resolves topartyhack.o
This is because TMP is not defined. Also note that the slash is missing after
$(TMP)
.5) make evaluates the target
partyhack.o
, and applies the implicit ruleg++ -c -o partyhack.o partyhack.cpp
It does not apply the rule you specified, namely
$(TEMP)/%.o : $(SRCDIR)/%.cpp
because TEMP is not defined, so this evaluates to/partyhack.o : ./partyhack.cpp
, and we are not building/partyhack.o
because we are not in the root directory.6) g++ does not find the include file, because the include directories were not passed to it, because your rule was not applied.
To fix this:
First, you need to define TEMP (see Nick Meyers answer).
Second, you need to change the definition of CPP_OBJS (as Paul R suggested).
This will work if you invoke make inside the directory CppPartyHack/PartyHack.
Make 似乎正在使用内置规则,而不是 *.cpp 文件的自定义规则。 (请参阅 make 输出中
-c
和-o
选项的顺序 - 它与您编写的不同)。检查 C++ 文件规则的定义。我最好的猜测是对 $(TEMP) 的引用正在抛弃它。这是在哪里定义的?
另请注意,对于 C++,通常变量
CXX
和CXXFLAGS
分别用于编译器程序和标志。CPP
用于 C 预处理程序,CPPFLAGS
用于预处理器标志,CFLAGS
用于 C 编译器标志。这可能就是make
在使用其内置 C++ 规则时不使用CFLAGS
的原因。Make appears to be using a built-in rule as opposed to your custom rule for *.cpp files. (See the ordering of the
-c
and-o
options in make's output -- it's not the same as the ones you wrote). Check the definition of your C++ file rule.My best guess is that the references to $(TEMP) are throwing it off. Where is this defined?
Also note that for C++, usually the variables
CXX
andCXXFLAGS
are used for the compiler program and the flags, respectively.CPP
is used for the C preprocessor program,CPPFLAGS
for preprocessor flags andCFLAGS
is used for the C compiler flags. That's probably whymake
is not using yourCFLAGS
when using its built-in C++ rules.也许您应该将 INCDIR 更改为绝对路径而不是相对路径? make 运行时,当前工作目录可能不是您想象的那样。
Maybe you should change the
INCDIR
to an absolute path rather than a relative one? The current working directory may not be what you think it is while make is running.显然某个地方存在问题,因为您的 CFLAGS 定义没有传递给 g++。
将:
CPP_OBJS = $(TEMP)partyhack.o
更改为:
CPP_OBJS = $(TEMP)/partyhack.o
除此之外,我没有看到任何错误。
There's obviously a problem somewhere because your CFLAGS definitions are not being passed to g++.
Change:
CPP_OBJS = $(TEMP)partyhack.o
to:
CPP_OBJS = $(TEMP)/partyhack.o
Other than that I don't see anything wrong.