Undefined reference to `main’. C compile error

Ukanah Dean
5 min readSep 25, 2022

--

Image of Compile error
Image of compile error

The “Undefined reference to main” error is one I found quite arduous and annoying to resolve during one of my learning days. I found it so annoying I told myself, if I could fix the error by myself (i.e. research, strictly through reading dev blogs, not StackOverflow), I would write the solution as my first medium post. I was able to fix it. So I am here to share my solution with anyone else out there with a similar problem. You might as well not be able to find this solution or something similar, but in my opinion, it would actually take longer than you would think. It took me quite some time and extra reasoning, I had to put a lot of different contributions to finally come to a solution which ran without errors on my GCC compiler using the -Wall -Werror -Wextra -pedantic -std=gnu89 features, which is unnecessary I know. I just wanted everything to be working perfectly.
So now that we have all that said let’s get to the main dining…

In my case, I have a folder/workspace containing three files.
1. main.c
2. add.c
3. main.h

In case you are a beginner in C development well no fears because so am I, I would be explaining why we have these extra files.
The function in this example takes two numbers from the main function in the main.c file to the add function in the add.c which adds the 2 numbers and returns the value back to the main function.

How C programs work:e
Programming in C follows the certain syntax/standard
- header file(s)
- file/function description
- function initialization
- function body

Header File:

The header file(s) included in the C file determine the function you can call/invoke in your working/current C program. Here is an example on a main.c file
Create a new file named main.c. Depending on how you created your file you should have a blank on your editor. So we now add the most common header file <stdio.h> so you would have your code looking exactly like this 👇

After that create the main function as so 👇:

In the above function, we import our stdio.h header file and give our main function a description. after that, we initialize our function and then create the function body.

In the body of the function,
on line 9: variable s, j and result are initialized
on line 11: var s is given a value of 4
on line 12: var j is given a value of 6
on line 14: var result is set to store the result of the invoked function add, and the values of s and j are passed as parameters to the function.

So now let's make our add function👇:

In our add.c file we add our stdio.h header file to be able to use the printf() function
on lines 2–7: we add our function description, and we set the parameter description for s and j.
on line 9: we initialize our add function and set the parameter as pointers to the integer variables passed from the main function.
on line 11: we initialize our result integer variable
on line 13: we added the s and j pointer and store the value to the result integer and
on line 15: we return the integer result as the result of the add function.

At this point, if we run our add function or main.c such as “gcc main.c” or “gcc add.c”

Image of Compile error

This is because the main.c file is yet to be compiled alongside the add.c file, we need to create a header file that contains files of the secondary functions. Functions such as the add function. This function usually ends with .h
So now let’s create the main.h file

For more information on creating your own header files, check out this link — Write your header file.
on line 4: we add the prototype of the add function to be able to link the add function to the main.h header file.

After this, we add the header file to both the main.c file and the add.c file to look like such👇

add.c file, main.c file

After doing this and the code looks exactly like the one above, what’s left is to compile our code. As much as I know there are two ways to do this. We’ll see through both of them

  • Compiling each .c file with -c parameter in the terminal which means to “Compile and Assemble, but do not link”. After doing that for the main.c file we get main.o output, similarly for add.c we get add.o as the result.
    Then we run the command “gcc main.o add.o -o add”. After we run that we get a file the executable file add.
  • Compiling both files simultaneously. To do this only run a one-line command “gcc main.c add.c -o add” and it creates an executable file add.

Then we execute the file by doing “./add” in our terminal

Conclusion
In this post, we learnt how to solve/evade the compile error “Undefined reference to main”. You can refer to the GCC online Documentation for more knowledge and information on C compilers. I’ll see you in my next post.

--

--

Ukanah Dean

Full Stack Web Developer, Upcoming Software Developer at ALX Africa… Python is definitely for me. I like Mob Psycho, C++. Nice to meet you.