Understanding how to call functions from another source file in C

Viewed 2377

I am trying to properly understand how to call C functions from another source file in C. I have a simple example that I am trying to get working. I have two functions, function1 and function2, which are contained in file.c. I am attempting to call function 2 from main.c bur receive the error "undefined reference to function 2".

main.c

#include <stdio.h>
#include <stdlib.h>
#include "file.h"
int main()
{
    int result = function2(3);
    printf("Result = %d  ", result);
    return 0;
}

file.c

int function1(int a)    /* Function 1  definition */
{
    int val = a*a;

    return val;
}

int function2(int val)    /* Function 2 definition */
{
    return val +5;
}

file.h

#ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED
extern int function1(int a) ;
extern int function2(int val);


#endif // FILE_H_INCLUDED

(I know that function1 and function2 can be combined into one function but I want to understand why this example isn't working! - thanks)

2 Answers
Related