C Programming

ERROR: Undefined Reference to a Function in C

We have seen and resolved a lot of exceptions while coding on any programming language. The Undefined reference is one of those exceptions that occurred mostly in the C language, as the name suggests that it would occur when some function definition is missing from the script. Thus, this article will give you some easy methods to resolve this error while starting with creating it. Make sure to log in from the Linux system and open the terminal to start implementing via the “Ctrl+Alt+T” shortcut method.

Note: We have already installed the gcc compiler in our running operating system.

Example 01:

One of the popular causes for the error undefined reference could be some name issue of a function. So, in this example, we will be seeing how an error could be generated and resolved using the function name. We will be creating a new “exp.c” file to add our C code to it within the shell terminal.

$ touch exp.c

The file has been successfully created in the home directory. To open this file, we need some editor applications. Therefore, we have been utilizing the Nano editor to open it.

$ nano exp.c

We kept our C code simple for easy understanding of our users. We have included a header file at the start of a code. After that, a main() function has been defined to print that we have been working on the main method right now. The spells of the function names are incorrect. Thus, it must display an undefined reference error while compilation of the code. We have Saved our code file and came back to the shell.

Make sure you have some C compiler configured in your Linux system. We have been using the everlasting GCC compiler here to compile the above C code. Upon compilation with GCC compiler, we have got the undefined reference to main() error as expected.

$ gcc exp.c

When we have executed the file, it says that there is no such file or directory.

$ ./a.out

Let’s correct the code and change the function’s name to main() as per the below image.

Thus, this time, when you compile and execute the file, it will work perfectly without any error, i.e., undefined reference to a function.

$ gcc exp.c
./a.out

Example 02:

Let’s have another example of the same error. So, open the same file and add an input-output standard header. A function prototype for the show() method has been declared after that. In the end, the main method has been defined to print some text and call the function show(), which has been declared earlier.

Upon compilation, we have got the error as an undefined reference to show(). This is because the show() method has only been declared but not defined in the code.

$ gcc exp.c

To resolve this error, we have updated the code once more. Added the function definition of the show() method after the main method. This method contains a single print statement within it.

When we compiled the file, the error was resolved, and the code got executed successfully.

$ gcc exp.c
./a.out

Example 03:

Another mistake most users make while programming is not paying attention to language syntax’s case sensitivity. This may also cause the error undefined reference to occur. So, let’s look at this issue in this example. We have used the same code so far. We have just used the different case syntax for function names, i.e., show and Show. The function definition contains the small letter name, while the function call contains capital letters. Save this code.

Used the GCC compiler to compile the exp.c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted.

$ gcc exp.c

To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further. Save this updated code to compile it.

When we compiled the code, at last, it successfully got compiled without any errors left. Thus, the execution of this file works fine as per the image below.

$ gcc exp.c
./a.out

Conclusion:

In this guide, we have seen how a simple mistake of a user can cause the undefined error reference to a function to occur. To resolve this error, we have implemented simple methods in the illustrations to make them understood well. We hope this article meets your requirements at its best.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content