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
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.
Also you need to check the return value of fopen
.
If it returns NULL
then your function readfile
(depending on capitalization) will crash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.