[英]Undefined reference error in a C program
I wrote a C program pgm.c
. 我写了一个C程序
pgm.c
It contains a function readfile.so
. 它包含一个功能
readfile.so
。 Also I wrote a header file do.h
. 我也写了一个头文件
do.h
Then I wrote this readfile
function in a separate program ini.c
and included do.h
to this program too. 然后,我写这个
readfile
在一个单独的程序功能ini.c
并列入do.h
这个节目了。 When I compiled pgm.c
I got the error "pgm.c:(.text+0x2e): undefined reference to readfile"
当我编译
pgm.c
,出现错误"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
由
gcc pgm.c
编译
C
is case sensitive; C
区分大小写;
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
. 另外,您需要检查
fopen
的返回值。
If it returns NULL
then your function readfile
(depending on capitalization) will crash 如果返回
NULL
则函数readfile
(取决于大小写)将崩溃
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.