【发布时间】:2016-07-23 08:21:05
【问题描述】:
当我编译以下程序时,出现以下错误
debian:~/uni/Ass0$ gcc fgetline.c -Wall -o enquote
/tmp/ccFnIr1N.o: In function `main':
fgetline.c:(.text+0xfe): undefined reference to `fgetline'
collect2: error: ld returned 1 exit status
经过一番阅读,我发现这个错误是由拼写错误或缺少库引起的。我不确定哪个库可能会丢失。
头文件“fgetline.h”
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int fgetline(FILE *fp, char *line, int max);
FILE *fp,*fpc;
#define max 30
char line[max+1];
fgetline.c
#include "fgetline.h"
int main(int argc, char* argv[])
{
if (argc !=3)
{
printf( "usage: enquote filetocopy filetowrite \n" );
exit(1);
}
fp = fopen (argv [1], "r");
if ( !fp)
{
printf("Couldn't open copy file: (%d) %s\n", errno, strerror(errno));
return -1;
}
fpc = fopen (argv [2], "r+");
if ( !fpc)
{
printf("Couldn't open write file: (%d) %s\n", errno, strerror(errno));
return -1;
}
while(1)
{
/* read string from first file*/
int length = fgetline(fp, line, 30 );
/* if fail to read, break from loop*/
if (length == -1)
{
break;
}
printf("\"%s\"\n",line); /*add "" around all strings read to new file*/
}
fclose (fp);
fclose (fpc);
return 0;
}
正如我所说,从我所知道的和我读过的其他内容来看,这不是拼写错误,它一定是缺少库,我使用 gcc fgetline.c -Wall -o enquote 进行编译 如果这有帮助的话。如果您发现其他错误,请随时指出,该程序是两个从命令行获取两个参数,循环通过第一个文件,并将每个字符串发送到第二个文件,并在字符串的开头和结尾添加“ .
【问题讨论】:
-
1) 不要在头文件中声明变量实例! 2) 不要使用制表符进行缩进。每个字处理器/编辑器都单独设置了制表位/制表符宽度,建议使用 4 个空格,因为即使使用可变宽度字体也足够宽,并且仍然允许整个页面有很多缩进级别。 3) 一致缩进:在每个左大括号'{'后缩进。在每个右大括号 '}' 之前取消缩进。
-
总是在每个头文件周围放置一个“包含保护”
-
在不使用头文件内容的情况下不包含头文件。
-
强烈建议您阅读
perror()的手册页并使用它,而不是混淆strerror()和errno -
关于这一行:
printf( "usage: enquote filetocopy filetowrite \n" );1) 这无法将错误消息输出到 stderr。 2)这对可执行文件名是什么做出了(可能是错误的)假设。强烈建议:fprintf( stderr, "USAGE: %s srcFile, destFile \n", argv[0] );