告诉 makefile 在不同目录中搜索 C 头文件
我已经尝试解决这个问题有一段时间了,现在我已经没有猜测/想法了。
我正在为学校的数值分析课编写一些 C 程序,其中许多程序都使用我事先编写的实用函数。我希望将实用程序“库”放在与类程序不同的目录中,但能够在需要时包含实用程序头文件。下面是我的“设置”的结构(+ = 目录/文件夹,- = 文件)
+Code
+Classes
+Fall_11
+Numerical_Programs
+Bisection
-.c and .h file(s) for bisection
+linux
-makefile
+util
+CompareFloats
- CompareFloats.h
所以基本上,我在目录 Bisection 中有 Bisection.c 和 Bisection.h。 Bisection.c 使用 util/CompareFloats 目录中 CompareFloats.h 中的浮点比较函数。
下面是我一直在尝试使用的 makefile:
###############################################################################
# makefile for Bisection test #
###############################################################################
#--------------------------------Macros----------------------------------------#
COMPILER = gcc
UTIL = ../../../../../util/CompareFloats
FLAGS = -ggdb -Wall -lm -I$(UTIL)
OBJECTS = ../Bisection.o ../BisectTest.o $(UTIL)/CompareFloats.o
OBJECT_DIR = ../OBJECTS
EXEC = BisectTest
all : $(EXEC)
test -d $(OBJECT_DIR) || mkdir $(OBJECT_DIR)
mv $(OBJECTS) $(OBJECT_DIR)
$(EXEC) : $(OBJECTS)
$(COMPILER) $(FLAGS) $(OBJECTS) -o $(EXEC)
#-----------------------------Dependencies--------------------------------------#
Bisection.o : ../Bisection.h
BisectTest.o: ../Bisection.h
CompareFloats.o: $(UTIL)/CompareFloats.h
clean:
rm -f $(EXEC) *.o $(OBJECT_DIR)/*.o
rmdir $(OBJECT_DIR)
我知道 -I 标志将允许在其他目录中搜索头文件。我认为我使用它是正确的,但我无法让它工作。我刚刚收到一个错误,指出 CompareFloats.h 不存在(我已经仔细检查了路径并且它是正确的)。在 Bisection.c 中,我
#include "CompareFloats.h"
像往常一样。我可以让它工作的唯一方法是让#include语句包含相对路径,例如
#include "../../../../util/CompareFloats/CompareFloats.h"
(它少了一个../,因为Bisection.c比makefile高一级)。
有什么建议吗?我真的很想只使用#include“CompareFloats.h”而不是相对路径。
如果我没有提供足够的信息,请告诉我。
谢谢。
I've been trying to figure this out for a little while now and I am at the point where I am out of guesses/ideas.
I am making some C programs for a numerical analysis class at school and many of the programs use utility functions that I've written beforehand. I'd like to have the utility "library" in a separate directory from the class programs, but be able to include the utility header files when needed. Below is the structure of my "setup" (+ = directory/folder, - = files)
+Code
+Classes
+Fall_11
+Numerical_Programs
+Bisection
-.c and .h file(s) for bisection
+linux
-makefile
+util
+CompareFloats
- CompareFloats.h
So basically, I have Bisection.c and Bisection.h within the directory Bisection. Bisection.c uses floating-point comparison functions that are within CompareFloats.h, in the util/CompareFloats directory.
Below is the makefile I've been trying to use:
###############################################################################
# makefile for Bisection test #
###############################################################################
#--------------------------------Macros----------------------------------------#
COMPILER = gcc
UTIL = ../../../../../util/CompareFloats
FLAGS = -ggdb -Wall -lm -I$(UTIL)
OBJECTS = ../Bisection.o ../BisectTest.o $(UTIL)/CompareFloats.o
OBJECT_DIR = ../OBJECTS
EXEC = BisectTest
all : $(EXEC)
test -d $(OBJECT_DIR) || mkdir $(OBJECT_DIR)
mv $(OBJECTS) $(OBJECT_DIR)
$(EXEC) : $(OBJECTS)
$(COMPILER) $(FLAGS) $(OBJECTS) -o $(EXEC)
#-----------------------------Dependencies--------------------------------------#
Bisection.o : ../Bisection.h
BisectTest.o: ../Bisection.h
CompareFloats.o: $(UTIL)/CompareFloats.h
clean:
rm -f $(EXEC) *.o $(OBJECT_DIR)/*.o
rmdir $(OBJECT_DIR)
I understand that the -I flag will allow header files to be searched for in other directories. I figured I was using it correctly, but I can't get it to work. I just get an error that CompareFloats.h does not exist (I've double checked the path and it is correct). Inside Bisection.c, I have
#include "CompareFloats.h"
like usual. The only way I can get this to work is to have the #include statement contain the relative path, like
#include "../../../../util/CompareFloats/CompareFloats.h"
(it has one less ../ because Bisection.c is one level up from the makefile).
Any suggestions? I'd really like to just have #include "CompareFloats.h" instead of the relative path.
If I am not providing enough information, please let me know.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以你的问题出在为 Bisection.o 生成的自动规则中。您可以执行以下两种操作之一:
1) 为
Bisection.o
制定规则:2) 将包含标志
-I$(UTIL)
附加到变量CFLAGS
(这些是传递给 C 编译器的标志)So your problem is in the auto rule generated for
Bisection.o
. You can do one of two things:1) make a rule for the
Bisection.o
:2) append the include flag
-I$(UTIL)
to the variableCFLAGS
(these are the flags passed to the C compiler)首先:
大多数人最终也会需要,
但你还不需要。
Start with:
Most people also end up needing
but you don't yet.