C - Undefined reference to function error with linked files

14,597

Complete compilation of the code is required in ubuntu terminal use the following

gcc normal.c main.c -o out -lm
Share:
14,597
Dinuka Lankaloka
Author by

Dinuka Lankaloka

A dynamic individual who strives to become a successful entrepreneur in the corporate world.

Updated on August 01, 2022

Comments

  • Dinuka Lankaloka
    Dinuka Lankaloka over 1 year

    I started implementing a large program. But I ran into a massive issue. So here is very simplified code of my program. I have a separate .c file for my functions which is normal.c the main program is main.c and I have linked those two with cal.h header file.

    main.c

    #include <stdio.h>
    #include "cal.h"
    
    void main()
    {
        int num1, num2, ans;
        num1=5;
        num2=5;
        ans=add(num1, num2);
        printf("%d" ,ans);
    }
    

    normal.c

    #include "cal.h"
    
    int add(int num1, int num2)
    {
        return num1+num2;
    }
    

    cal.h

    #ifndef CAL_H_INCLUDED
    #define CAL_H_INCLUDED
    #include <errno.h>
    
    int add(int num1, int num2);
    
    #endif // CAL_H_INCLUDED
    

    but when I compile this, it gives out the error ..\main.c|10|undefined reference to `add'|

    I'm using CodeBlocks v.13.12 in Windows 8.1 Any answer for this question is much appreciated. I tried with CodeLite as well, but the same error occurs. Thank you!