Revisiting compiling C program consisting of mulitple program files

Example:   the following C program that consists of 2 program files:

main1.c func1.c
#include <stdio.h>

// Notice: no function declaration used

int main(int argc, char *argv[])
{
    int a = 4, b;

    b = square(a);
    printf("square(%d) = %d\n", a, b);
}
int square( int x )        
{
    int r;              

    r = x * x;      
    return ( r );  
}



  

Commands used to compile this C program:

  gcc  -c  main1.c                 # Compile main1.c
  gcc  -c  func1.c		   # Compile func1.c

  gcc -o main1   main1.o  func1.o  # Link 

DEMO: demo/C/set4/main1.c + func1.c

Revisiting compiling C program consisting of mulitple program files

The resulting program ran correctly because the implicit function prototype assumption was correct

main1.c func1.c
#include <stdio.h>

// Notice: no function declaration used

int main(int argc, char *argv[])
{
    int a = 4, b;
                   // Assumed:
    b = square(a); //  int square(int)
    printf("square(%d) = %d\n", a, b);
}
int square( int x )        
{
    int r;              

    r = x * x;      
    return ( r );  
}



  

 

Run time errors will occur when the implicit function prototype assumption was incorrect

We will see an example next

Revisiting the data type information problem in the compilation of multiple files C programs

Suppose the data type of the input parameter x of square( ) was float

main2.c func2.c
#include <stdio.h>

// Notice: no function declaration used

int main(int argc, char *argv[])
{
    int a = 4, b;
                   // Assumed:
    b = square(a); //  int square(int)
    printf("square(%d) = %d\n", a, b);
}
int square( float x )        
{
    int r;              

    r = x * x;      
    return ( r );  
}



  
 

The implicit function prototype assumption used by the C compiler is now incorrect

  • The resulting executable program will have run time errors

    (Because main( ) passes a value in 2s complement code and square( ) assumes it is a IEEE 754 code)

DEMO: demo/C/set4/main2.c + func2.c

Explanation of the runtime error

The C compiler compiles each C program file separately:

Recall:   data type information in one C program is not used in the compilation of other C programs

Explanation of the runtime error

When the C compiler find the square(x) function call it assumes: square( int )

(This is the C compiler's implicit function declaration mechanism)

Explanation of the runtime error

The implicit function declaration was wrong:

which results in the run time error (because a wrong code was used to represent the values)

How to fix the data type information problem in the compilation of multiple files C programs

Question:   how can we fix this missing data type problem:

main2.c func2.c
#include <stdio.h>

// Notice: no function declaration used

int main(int argc, char *argv[])
{
    int a = 4, b;

    b = square(a); // Use function
    printf("square(%d) = %d\n", a, b);
}
int square( float x )        
{
    int r;              

    r = x * x;      
    return ( r );  
}



  
 

DEMO: demo/C/set4/main2.c + func2.c

How to fix the data type information problem in the compilation of multiple files C programs

Provide the C compiler with the data type information by using a (function) declaration:

main2.c func2.c
#include <stdio.h>

int square( float x ) ; // Declaration 

int main(int argc, char *argv[])
{
    int a = 4, b;

    b = square(a); // Use function
    printf("square(%d) = %d\n", a, b);
}
int square( float x )        
{
    int r;              

    r = x * x;      
    return ( r );  
}



  
 

DEMO: demo/C/set4/main2.c + func2.c

Final comments

  • In the previous slides, I presented the case that:

      • Data type information is essential for the correctness of a C program compilation

      • When you see the message:

           warning: implicit declaration of function ... 

        You must take care of the missing function declaration(s)

  • Good news:

      • Declaring functions and global variables that are defined inside a C program file is very easy if you use header files