0

I am having an issue with a makefile for something I am making. My makefile looks like this

bag: main.o bow.o
    gcc bow.o main.o -o bag
main.o: main.c bow.h
    gcc -Wall -ansi -pedantic main.c -o main.o
bow.o: bow.c bow.h
    gcc -Wall -ansi -pedantic -c bow.c -o -bow.o

I also have a header file called "bow.h" that is used in both bow.o and main.o. bow.h consists of 8 function definitions and 2 structs, bow.c contains the 8 functions and NO MAIN file. main.c is suppose to be a minimal main file so it only consists of

#include "bow.h"

When I run my makefile in the Terminal with

make

I get this message

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'bag' failed
make: *** [bag] Error 1

What exactly does this mean, how is it caused and how can I fix it?

4
  • 5
    Do any of the source files contain a main function? you do know that a C program must have a main function, as that's where the execution start? The error message simply says that the linker can't find the main function. Commented Jan 30, 2018 at 7:46
  • Not according to main.c is suppose to be a minimal main file so it only consists of #include "bow.h" Commented Jan 30, 2018 at 7:48
  • 1
    main.c needs a main function (that calls one or more of the functions in bow.c), and the line that compiles main.c needs a -c option. Commented Jan 30, 2018 at 7:49
  • "bow.c contains ... and NO MAIN file." Do you mix up "file" and "function"? What is a minimal main file if there is no main function in it? There is no such thing as a "main file" in C. All files are same. It all depents on the definitiones inside. Commented Jan 30, 2018 at 8:44

1 Answer 1

2

Even a minimal program (executable) needs a point to start. For a C program, this is the main() function. Thus, the linker seeks for that function (more precisely, it links the start-up object where main is an unresolved symbol), does not find it, and issues an error.

Thus, you have to provide a main(). Alternatively, you may not generate an executable but a library.

Sign up to request clarification or add additional context in comments.

2 Comments

This is right answer, however for advanced users, this is partially true. Default is main(), but it is possible to modify an entry point (for whatever reason). See also: stackoverflow.com/questions/7494244/…
You are right, but this is a compiler feature. The C standard say that ˋmain()ˋ is called at startup (cf. eg. C11 5.1.2.2.1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.