(.text + 0x20): undefined reference to "main" and undefined reference to a function

My question is how to make makefile work without errors. The first question I have is with an undefined link to the main one. I have the main file my.pro.exe as a function. The second problem is the undefined link to SearchCustomer ().

Mistake:

bash-4.1$ make gcc -Wall -c producer.c shared.h gcc -Wall -c consumer.c shared.h gcc -Wall -c AddRemove.c shared.h gcc -pthread -Wall -o producer.o consumer.o AddRemove.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' AddRemove.o: In function `AddRemove': AddRemove.c:(.text+0xb1): undefined reference to `SearchCustomer' AddRemove.c:(.text+0x1e9): undefined reference to `SearchCustomer' AddRemove.c:(.text+0x351): undefined reference to `SearchCustomer' collect2: ld returned 1 exit status make: *** [producer] Error 1 

Makefile:

 COMPILER = gcc CCFLAGS = -Wall all: main debug: make DEBUG=TRUE main: producer.o consumer.o AddRemove.o $(COMPILER) -pthread $(CCFLAGS) -o producer.o consumer.o AddRemove.o producer.o: producer.c shared.h $(COMPILER) $(CCFLAGS) -c producer.c shared.h consumer.o: consumer.c shared.h $(COMPILER) $(CCFLAGS) -c consumer.c shared.h AddRemove.o: AddRemove.c shared.h $(COMPILER) $(CCFLAGS) -c AddRemove.c shared.h ifeq ($(DEBUG), TRUE) CCFLAGS += -g endif clean: rm -f *.o 
+13
c gcc compilation compiler-errors makefile
source share
2 answers

This rule is

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

not this way. It says to create a file called Manufacturer.o (with -o producer.o ), but you want to create a file called main . I apologize for the scream, but ALWAYS USE $ @ TO PURPOSE THE TASK :

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

As Shabaz rightly notes, gmake experts will also use $^ , which applies to all the premises of the rule. As a rule, if you repeat a line or a name, you are doing it wrong and should use a variable, whether built-in or created by you.

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

This error means that when linking, the compiler cannot find the definition of the main() function anywhere.

In your makefile, the main rule expands to something like this.

 main: producer.o consumer.o AddRemove.o gcc -pthread -Wall -o producer.o consumer.o AddRemove.o 

According to the gcc manual page man page , using the -o switch is described below

File -o Put output in file. This applies regardless of which output is output, whether it is an executable file, an object file, an assembler file, or pre-processed C code. If -o not specified, the default executable is placed in a.out .

This means that gcc will put the output in the file name indicated immediately next to the -o switch. So here, instead of linking all the .o files together and creating a binary [ main , in your case], it creates a binary like producer.o , linking the other .o files. Please fix this.

+7
source share

All Articles