生成文件子目录
我正在尝试按如下方式组织我的项目目录
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
-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 beshell/src
. The latter addsshell
, 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.
您的编译器正在查找 inc/shell.h,但它不应该查找。通常,源文件中不应包含构建系统相关路径,但构建系统的详细信息应通过编译器标志来定义。
也就是说,您的源文件 src/shutil.c 应该如下所示:
并且您的编译器调用是相同的,并且 -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:
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.
好吧,我经历了一些彻底的挫折,但我现在明白了。
我更改了顶部的定义以包含文件路径,如下所示:
看来关键是 -I./inc,而不是我预期的 -Iinc。
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:
And it seems the key was -I./inc, instead of -Iinc as I expected.