gpt4 book ai didi

c++ - 使用 makefile 链接来自不同目录的 .h 文件和 .c 文件

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:15 25 4
gpt4 key购买 nike

我正在尝试使用 um.c 从当前文件夹编译一个名为 um 的程序并包括一些外部实现。 .h 文件位于一个目录中,但实现这些 .h 文件的 .c 文件位于不同的目录中。下面是我的生成文件。编译器似乎知道在哪里寻找 .h 文件。但是,链接器失败并产生错误:

ld: symbol(s) not found for architecture x86_64
clang: fatal error: linker command failed with exit code 1

生成文件:

# define the C compiler to use
CC = gcc

# define any compile-time flags
CFLAGS = -g -O -Wall -Wextra -Werror -Wfatal-errors -std=c99 -pedantic

# define any directories containing header files other than /usr/include
INCLUDES = -I/Users/nguyenmanhduc/Documents/C\ library/cii/include

# define library paths in addition to /usr/lib
LFLAGS = -L/Users/nguyenmanhduc/Documents/C\ library/cii/src

# define any libraries to link into executable:
LIBS = -lm

# define the C source files
SRCS = um.c

# define the C object files
OBJS = $(SRCS:.c=.o)

# define the executable file
MAIN = um

.PHONY: depend clean

all: $(MAIN)
@echo Simple compiler named um has been compiled

$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

clean:
$(RM) *.o *~ $(MAIN)

depend: $(SRCS)
makedepend $(INCLUDES) $^

这个问题可能看起来很奇怪,因为我对 makefile 没有什么经验,但是有没有一种方法可以使用 makefile 链接不同文件夹中的 .h 和 .c 文件。

谢谢!

最佳答案

这里的问题不是您的附加 .c 文件位于不同的目录中:而是您没有告诉 Makefile 它们存在根本!

这是您列出源输入的地方(我猜您没有看到评论?):

# define the C source files
SRCS = um.c

添加其他 .c 文件,其编译后的 .o 是您要链接的内容。

例如:

# define the C source files
SRCS = um.c ../wot.c ../hah/lol.c

没有硬性规定,但是按照您构建此 Makefile 的方式,这些相对路径应该可以很好地解析。

关于c++ - 使用 makefile 链接来自不同目录的 .h 文件和 .c 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36381272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com