使用 makefile 时未定义的引用

undefined reference when using makefile

我有一个给定的 makefile,是我们教授写的。 `

SHELL  = /bin/bash
CC     = gcc
CFLAGS = -Wall -W -std=c99 -pedantic
LIBS   =

# Rajouter le nom des executables apres '=', separes par un espace.
# Si une ligne est pleine, rajouter '\' en fin de ligne et passer a la suivante.

# To compile without bor-util.c file 
EXECS = main

# To compile with bor-util.c file 
EXECSUTIL = 

# To compile with bor-util.c & bor-timer.c files
EXECSTIMER = 


.c.o :
    $(CC) -c $(CFLAGS) $*.c

help ::
    @echo "Options du make : help all clean distclean"

all :: $(EXECS) $(EXECSUTIL) $(EXECSTIMER)

$(EXECS) : %: %.o 
    $(CC) -o $@ $@.o $(LIBS)

$(EXECSUTIL) : %: %.o bor-util.o
    $(CC) -o $@ $@.o bor-util.o $(LIBS)

$(EXECSTIMER) : %: %.o bor-util.o bor-timer.o
    $(CC) -o $@ $@.o bor-util.o bor-timer.o $(LIBS)

clean ::
    \rm -f *.o core

distclean :: clean
    \rm -f *% $(EXECS) $(EXECSUTIL) $(EXECSTIMER)
`

我们在这个项目中所要做的就是将我们的代码写在其他文件中,然后像往常一样使用这个 makefile 进行编译。 我写了一个 helloWorld 函数来测试。我有3个文件 FUNCTIONS.C

#include <stdio.h>
#include "functions.h"


    void printMsg(){
        printf("Hello World !");
    }

FUNCTIONS.H

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void printMsg();
#endif /* FUNCTIONS_H */

还有一个 MAIN.C 文件来测试所有内容

#include "functions.h"


int main(){

    printMsg(); 
    return 0;
}

并且我已经将 main 添加到 makefile 中。但是我在编译时收到此错误消息

gcc -o main main.o 
main.o: In function `main':
main.c:(.text+0xa): undefined reference to `printMsg'
collect2: error: ld returned 1 exit status
Makefile:32: recipe for target 'main' failed
make: *** [main] Error 1

有谁知道解决办法吗?谢谢

使用这个 makefile 就可以了,而且更 comprehensible/better

GREEN   = 3[1;32m

WHITE   = 3[0;m

NAME    = main

CC      = gcc -g

RM      = rm -f

SRCS    = functions.c    \
          main.c

OBJS    = $(SRCS:.c=.o)

CFLAGS = -I ./include/
CFLAGS += -W -Wall -Wextra

all: $(NAME)

$(NAME): $(OBJS)
         @$(CC) $(OBJS) -o $(NAME) $(LDFLAGS)
         @printf "\n[$(GREEN)OK$(WHITE)] Binary : $(NAME)\n"
         @echo "-------------------\n"

%.o :    %.c
         @$(CC) $(CFLAGS) -c -o $@ $<
         @printf "[$(GREEN)OK$(WHITE)] $<\n"

clean:
        $(RM) $(OBJS)

fclean: clean
    $(RM) $(NAME)

re: fclean all

.PHONY: all clean fclean re

functions.h 文件:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void printMsg();

#endif /* !FUNCTIONS_H_ */

也一样main.c。您不需要在 function.c

中包含 "functions.h"

Allez l'om,

尼古拉斯 :)

错误信息很清楚:linker 没有找到 printMsg 函数。这是完全正常的:执行的 link 命令是:

gcc -o main main.o

看到了吗?没有 functions.o 实现 printMsg 功能的痕迹。要解决此问题,您必须 link 使用此命令:

gcc -o main main.o functions.o

问题是你的 Makefile 没有提到 functions.o 作为 main 的先决条件,它也没有在配方中使用它。要么你没看懂教授的说明(谁没让你加functions.cfunctions.h),要么你忘了他还说明了如何更新Makefile,或者他的Makefile和他的不兼容自己的指示。在后两种情况下,您可以通过更改 $(EXECS):

的规则来调整 Makefile
$(EXECS) : %: %.o functions.o
    $(CC) -o $@ $^ $(LIBS)

$^ 扩展为所有先决条件的列表,也就是说,在您的情况下,main.o functions.o。这条新规则将:

  1. 如果 main.ofunctions.o 已更改,请重新构建 main
  2. Link main.o functions.o.

警告:如果 $(EXECS) 中列出的其他可执行文件不依赖于 functions.o,或者依赖于其他目标文件,或者如果您有更多其他文件,例如 functions.o,你需要一些更复杂的东西。问一个新问题。

注意:由于 SO 是英文,所以最好将示例代码中的法语注释翻译成英文。我建议:

Add the executable names after '=', separated by one space. If a line is full, add a '\' at the end and continue on the next line.

最后一点:字母大小写很重要。如果您的文件是 functions.c,请不要在您的问题中输入 FUNCTIONS.C。与其他文件名相同。