(.text+0x20): undefined reference to `main' and undefined reference to function

前端 未结 2 1485
遥遥无期
遥遥无期 2020-12-15 05:45

I am having issue getting my makefile to work without errors. The first issue i have is with an undefined reference to main. I have main in my producer.c file as a function.

2条回答
  •  一整个雨季
    2020-12-15 06:08

    This rule

    main: producer.o consumer.o AddRemove.o
       $(COMPILER) -pthread $(CCFLAGS) -o producer.o consumer.o AddRemove.o
    

    is wrong. It says to create a file named producer.o (with -o producer.o), but you want to create a file named main. Please excuse the shouting, but ALWAYS USE $@ TO REFERENCE THE TARGET:

    main: producer.o consumer.o AddRemove.o
       $(COMPILER) -pthread $(CCFLAGS) -o $@ producer.o consumer.o AddRemove.o
    

    As Shahbaz rightly points out, the gmake professionals would also use $^ which expands to all the prerequisites in the rule. In general, if you find yourself repeating a string or name, you're doing it wrong and should use a variable, whether one of the built-ins or one you create.

    main: producer.o consumer.o AddRemove.o
       $(COMPILER) -pthread $(CCFLAGS) -o $@ $^
    

提交回复
热议问题