告诉 makefile 在不同目录中搜索 C 头文件

发布于 2024-12-02 21:45:16 字数 2049 浏览 1 评论 0原文

我已经尝试解决这个问题有一段时间了,现在我已经没有猜测/想法了。

我正在为学校的数值分析课编写一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-12-09 21:45:16

所以你的问题出在为 Bisection.o 生成的自动规则中。您可以执行以下两种操作之一:

1) 为 Bisection.o 制定规则:

Bisection.o :       ../Bisection.h
        gcc -c -o ../Bisection.o -I$(UTIL) ../Bisection.c 

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:

Bisection.o :       ../Bisection.h
        gcc -c -o ../Bisection.o -I$(UTIL) ../Bisection.c 

2) append the include flag -I$(UTIL) to the variable CFLAGS (these are the flags passed to the C compiler)

挖鼻大婶 2024-12-09 21:45:16

首先:

CFLAGS=$(CFLAGS) -I$(UTIL)
CXXFLAGS=$(CXXFLAGS) -I$(UTIL)

大多数人最终也会需要,

LDFLAGS=$(LDFLAGS) -I$(UTIL)

但你还不需要。

Start with:

CFLAGS=$(CFLAGS) -I$(UTIL)
CXXFLAGS=$(CXXFLAGS) -I$(UTIL)

Most people also end up needing

LDFLAGS=$(LDFLAGS) -I$(UTIL)

but you don't yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文