我的设置是这样的:
src/
dir1/
header.h
dir11
(several files, every including header.h)
dir2/
(and so on)
但是链接器告诉我 undefined reference (在子目录“dir11”中定义的函数)
。标题如下所示:
#ifndef header_h
#define header_h
/* some includes, all protected via ifndef-define-endif */
/* function prototypes for all functions in the 'dir11' subdir */
#endif // header_h
生成文件:
# some variables are defined before...
TARGET=mytarget
SOURCES= ${shell find ${SRC} -type f -name '*.c'}
OBJECTS=${foreach x, $(basename $(SOURCES)), $(x).o}
compile: ${OBJECTS}
%.o: %.c
${CC} -c ${CFLAGS} ${HEADERS} $^ -o $@
link:
${LD} ${OBJECTS} -o ${TARGET}
如何解决这个问题?为子目录中的每个文件编写 header 是我想要的最后一种方法,因为其中大约有 50 个文件!
编辑:
也许这很有用:header.h 也包含在其他地方来调用函数!当包含 elsewere 时,我是否必须将此函数声明为 extern
?
最佳答案
你所有的包含守卫定义都不同吗?如果没有,您将收到未定义的引用警告/错误,具体取决于您的编译器设置
关于c - 某些 .c 文件的一个 header 会导致链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14592389/