生成文件子目录

发布于 2024-12-14 03:45:54 字数 931 浏览 0 评论 0原文

我正在尝试按如下方式组织我的项目目录

shell
|inc/[头文件]
|obj/[目标文件]
|src/[源文件]
|生成文件
|可执行文件

根文件夹中的所有内容都可以正确编译,但我在修改 makefile 以适应不同目录中的所有内容时遇到了困难。

这是我的 makefile 在没有目录的情况下的样子

OBJS = shutil.o parser.o sshell.o
HEADER_FILES = shell.h parser.h
EXECUTABLE = sshell
CFLAGS = -Wall
CC = gcc
#Create main executable
$(EXECUTABLE): $(OBJS)
    $(CC) -o $(EXECUTABLE) $(OBJS)
#Create object files
%.o: %.c
$(CC) $(CFLAGS) -I.  -c -o $@ $<
$(OBJS) : $(HEADER_FILES)

我尝试将目录添加到定义中(对于 obj/ 和 inc/ 目录)

OBJS = obj/shutil.o obj/parser.o obj/sshell.o

并将 %.o 和 %.c 的每个实例修改为 obj/%.o 和 src/%。据

我所知,一切都应该没问题,但是当我从根目录进行 make 时,我收到此错误

gcc -Wall -Iinc -c -o obj/shutil.o src/shutil.c
src/shutil.c:8:23: fatal error: inc/shell.h: No such file or directory
compilation terminated.

有人能发现我在这里做错了什么吗?我已经为此苦苦挣扎好几天了。

I'm attempting to organize my project directory as follows

shell
|inc/[header files]
|obj/[object files]
|src/[source files]
|Makefile
|Executable

Everything compiles without error with everything in the root folder, but I'm running into difficulty modifying my makefile to accommodate having everything in different directories.

Here's how my makefile looked without directories

OBJS = shutil.o parser.o sshell.o
HEADER_FILES = shell.h parser.h
EXECUTABLE = sshell
CFLAGS = -Wall
CC = gcc
#Create main executable
$(EXECUTABLE): $(OBJS)
    $(CC) -o $(EXECUTABLE) $(OBJS)
#Create object files
%.o: %.c
$(CC) $(CFLAGS) -I.  -c -o $@ 
lt;
$(OBJS) : $(HEADER_FILES)

I've tried adding the directories into the definitions (for the obj/ and inc/ directories)

OBJS = obj/shutil.o obj/parser.o obj/sshell.o

And modified every instance of %.o and %.c to obj/%.o and src/%.c

Everything should be okay as far as I can tell, but I get this error when I go to make from the root directory

gcc -Wall -Iinc -c -o obj/shutil.o src/shutil.c
src/shutil.c:8:23: fatal error: inc/shell.h: No such file or directory
compilation terminated.

Can anyone find what I'm doing wrong here? I've been struggling with this for days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ぃ弥猫深巷。 2024-12-21 03:45:54

-I. 更改为 -I${CURDIR}。前者将正在编译的源目录添加到包含路径列表中,该目录为 shell/src。后者添加了 shell,这可能就是您想要的。

您还可以查看使用类似规则构建多个可执行文件 用于微型非递归 make 框架。

Change -I. to -I${CURDIR}. The former adds the directory of the source being compiled to the include path list, which would be shell/src. The latter adds shell, this is probably what you want.

You may also take a look at Building multiple executables with similar rules for a micro non-recursive make framework.

晨与橙与城 2024-12-21 03:45:54

您的编译器正在查找 inc/shell.h,但它不应该查找。通常,源文件中不应包含构建系统相关路径,但构建系统的详细信息应通过编译器标志来定义。

也就是说,您的源文件 src/shutil.c 应该如下所示:

#include "shell.h"

并且您的编译器调用是相同的,并且 -Iinc 传递包含路径。

通过将路径保留在源文件之外,头文件的实现和安装要简单得多。

Your compiler is looking for inc/shell.h, which it shouldn't. Generally, no build system dependent path should be in the source file, but the details of the build system should rather be defined via compiler flags.

That is, your source file src/shutil.c should look like:

#include "shell.h"

and your compiler invocation be the same, with the -Iinc passing the include path.

By keeping the paths out of the source files, implementation and installation of header files is much simpler.

徒留西风 2024-12-21 03:45:54

好吧,我经历了一些彻底的挫折,但我现在明白了。
我更改了顶部的定义以包含文件路径,如下所示:

OBJS = obj/shutil.o obj/parser.o obj/sshell.o
HEADER_FILES = inc/shell.h inc/parser.h

看来关键是 -I./inc,而不是我预期的 -Iinc。

obj/%.o: src/%.c
    $(CC) $(CFLAGS) -I./inc -c -o $@ 
lt;

Okay, it took some thorough frustration, but I got it now.
I changed my definitions at the top to include the file paths, as so:

OBJS = obj/shutil.o obj/parser.o obj/sshell.o
HEADER_FILES = inc/shell.h inc/parser.h

And it seems the key was -I./inc, instead of -Iinc as I expected.

obj/%.o: src/%.c
    $(CC) $(CFLAGS) -I./inc -c -o $@ 
lt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文