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?

Clayton D
  • 37
  • 7
  • 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. – Some programmer dude Jan 30 '18 at 07:46
  • Not according to `main.c is suppose to be a minimal main file so it only consists of #include "bow.h"` – Mawg says reinstate Monica Jan 30 '18 at 07: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. – user3386109 Jan 30 '18 at 07: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. – Gerhardh Jan 30 '18 at 08:44

1 Answers1

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.

Matthias
  • 8,018
  • 2
  • 27
  • 53
  • 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: https://stackoverflow.com/questions/7494244/how-to-change-entry-point-of-c-program-with-gcc – Kuchara Jan 30 '18 at 13:35
  • 1
    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). – Matthias Jan 30 '18 at 13:42
  • Good to know! Thanks. – Kuchara Jan 31 '18 at 16:14