c - 某些 .c 文件的一个 header 会导致链接器错误

标签 c header linker makefile

我的设置是这样的:

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/

相关文章:

c++ - 为 Arduino 编写库

c++ - 运算符 new 和 delete 重载作用域

将 *.raw 转换为 *.tiff

C检查目录是否存在的更快方法

jquery - jQuery ajax 接受 attrib 有什么意义?它实际上有什么作用吗?

php检查gravatar是否存在header错误

c++ - 从 Xcode 链接到 Boost

c++ - 如果出现链接器错误 "undefined reference",如何找到符号的调用例程?

c - Objective-C运行时如何实例化根元类和其他类描述?

c - 仅在第一次迭代时执行