PDA

View Full Version : unable to compile C program with getline() using gcc




l0uismustdie
Jan 15, 2012, 08:46 AM
Hello, I am trying to run a C program using getline() and am receiving the following error message:

14:42@***:~/Documents/c$ gcc -o 3n1 3n1.c
Undefined symbols:
"_getline", referenced from:
_main in ccMhB6EQ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

The program is as simple as possible:

#include <stdio.h>

main()
{
int x;
char *line;

x = getline(line,100,stdin);
}


I used the definition from getline() on this site: https://wiki.cs.columbia.edu:8443/display/res/getline%28%29+missing
placed this in a getline.c file and included the getline.c file in my program and this resulted in a segmentation fault upon execution.

Thanks to anyone who can point me in the right direction.



robvas
Jan 15, 2012, 10:17 AM
Hello, I am trying to run a C program using getline() and am receiving the following error message:

14:42@***:~/Documents/c$ gcc -o 3n1 3n1.c
Undefined symbols:
"_getline", referenced from:
_main in ccMhB6EQ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

The program is as simple as possible:

#include <stdio.h>

main()
{
int x;
char *line;

x = getline(line,100,stdin);
}


I used the definition from getline() on this site: https://wiki.cs.columbia.edu:8443/display/res/getline%28%29+missing
placed this in a getline.c file and included the getline.c file in my program and this resulted in a segmentation fault upon execution.

Thanks to anyone who can point me in the right direction.

getline is included in OS X (at least it is in Lion, that page may have been from 'the old days')

From a Terminal type 'man getline'

NAME
getdelim, getline -- get a line from a stream

LIBRARY
Standard C Library (libc, -lc)

SYNOPSIS
#include <stdio.h>

ssize_t
getdelim(char ** restrict linep, size_t * restrict linecapp, int delimiter,
FILE * restrict stream);

ssize_t
getline(char ** restrict linep, size_t * restrict linecapp, FILE * restrict stream);

EXAMPLES
The following code fragment reads lines from a file and writes them to standard output. The
fwrite() function is used in case the line contains embedded NUL characters.

char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linecap, fp)) > 0)
fwrite(line, linelen, 1, stdout);



Per the man page, it looks like your first two arguments to the function are not right.

l0uismustdie
Jan 15, 2012, 10:20 AM
Hmm...this may be the case. I am running Snow Leopard though.

15:57@***:~/Documents/c$ man getline
No manual entry for getline

robvas
Jan 15, 2012, 10:40 AM
Hmm...this may be the case. I am running Snow Leopard though.

15:57@***:~/Documents/c$ man getline
No manual entry for getline


Are you allocating the pointer any memory to hold the input data?

http://members.shaw.ca/ipatters/BeginC_9.html

Mr. Retrofire
Jan 15, 2012, 11:35 AM
Hello, I am trying to run a C program using getline() and am receiving the following error message:

man stdio

tells us, that this function is not available in the Standard C lib (10.6.x). So write your own or use this:
getdelim.c
and
getline.c
from
http://www.opensource.apple.com/source/cvs/cvs-42/cvs/lib/
(contains other interesting parts)

Should work without problems. Most configure scripts of open source programs check the availability of getline, which is the reason, why you see no GCC errors regarding getline in most cases.

:-)

Sander
Jan 15, 2012, 01:30 PM
It looks like you have to allocate the buffer for the line yourself. So change your code to


#include <stdio.h>
#define MAX_LINE 100

int main(void)
{
int x;
char line[MAX_LINE];

x = getline(line, MAX_LINE, stdin);
return 0;
}

You were getting a segmentation fault because the getline() function you added to your program assumed that the buffer passed to it for it to fill was allocated by you beforehand. Instead, it was trying to poke the characters in no man's land.

gnasher729
Jan 15, 2012, 02:55 PM
#include <stdio.h>

main()
{
int x;
char *line;

x = getline(line,100,stdin);
}



"line" is an unitialised variable. This isn't going to work. If you are a beginner, you should say so to get help appropriate to your level. If you are not a beginner, what do you think is going to happen when getline stores a line of text where that uninitialised variable points to? A crash is the best thing that can happen.