user39133
user39133

Reputation: 93

Undefined reference error in a C program

I wrote a C program pgm.c. It contains a function readfile.so. Also I wrote a header file do.h. Then I wrote this readfile function in a separate program ini.c and included do.h to this program too. When I compiled pgm.c I got the error "pgm.c:(.text+0x2e): undefined reference to readfile"

My codes are :

do.h
    extern int readfile( FILE *in );

pgm.c
    #include<stdio.h>
    #include"do.h"
    int main(int argc,char **argv)
    {
     FILE *in;
     in=fopen(argv[1],"r");
     readfile(in);
    }

ini.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    #include <float.h> 
    #include"do.h"
    int c;
    int readfile( FILE *in )
    {
    while( c == '#' || c == '\n' )  
    {
      if( c == '#' ) 
       {
        while( ( c = getc(in) ) != '\n' );
       }
      if( c == '\n' ) 
       c = getc( in ); 
     }
      if( c == EOF )
      break;
      else
      {
      printf("hai");
      }
    }
    return 1;
    }

Is there any error in my programs? compiling by gcc pgm.c

Upvotes: 1

Views: 1845

Answers (2)

Ed Heal
Ed Heal

Reputation: 60027

Also you need to check the return value of fopen.

If it returns NULL then your function readfile (depending on capitalization) will crash

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 181047

C is case sensitive;

extern int readfile( FILE *in );

doesn't declare the same function as;

int readFile( FILE *in )

Pick a casing and use it everywhere for the same function.

Upvotes: 4

Related Questions