1

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user39133
  • 93
  • 1
  • 7
  • no need to declare function `extern`, they're extern by default – mangusta Mar 09 '14 at 08:43
  • 1
    where is the command line you use to compile & link ? add the complete line, because it looks like you don't compile and link with "ini.c" – alexbuisson Mar 09 '14 at 08:43
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Apr 09 '14 at 11:05

2 Answers2

4

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.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

Also you need to check the return value of fopen.

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

Ed Heal
  • 59,252
  • 17
  • 87
  • 127