Reputation: 178
When I compile the following program,I am given the following error
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
After a bit of reading, I have found this error is caused by either, a spelling mistake or a missing library. I am unsure which library might be missing.
Header file "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;
}
As I said, not a spelling error from what I can tell and from what else I have read it must be a missing lib, I compile using gcc fgetline.c -Wall -o enquote if that helps at all. If you see other errors please feel free to point them out, the program is two take two arguments from the command line, loop through the first file, and send each string to the second file with " added to the start and finish of the string.
Upvotes: 1
Views: 4729
Reputation: 121387
The error message says you are calling the function fgetline()
from main()
but the definition of fgetline()
couldn't be located. This is probably because you didn't link with the library that fgetline()
is a part of or didn't compile the object file containing fgetline()
when compiling main().
The header you include only contains a declaration but the definition of getline()
is needed to produce the final executable. Perhaps, you meant to use POSIX getline?
If it's your own function then you need to compile with its definition.
Upvotes: 3