Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 15, 2012, 08:46 AM   #1
l0uismustdie
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
unable to compile C program with getline() using gcc

Hello, I am trying to run a C program using getline() and am receiving the following error message:
Code:
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:
Code:
#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/di...%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.
l0uismustdie is offline   0 Reply With Quote
Old Jan 15, 2012, 10:17 AM   #2
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Quote:
Originally Posted by l0uismustdie View Post
Hello, I am trying to run a C program using getline() and am receiving the following error message:
Code:
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:
Code:
#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/di...%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'
Code:
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.

Last edited by OllyW; Jan 15, 2012 at 11:40 AM. Reason: clean up after moving thread
robvas is offline   0 Reply With Quote
Old Jan 15, 2012, 10:20 AM   #3
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Hmm...this may be the case. I am running Snow Leopard though.
Code:
15:57@***:~/Documents/c$ man getline
No manual entry for getline
l0uismustdie is offline   0 Reply With Quote
Old Jan 15, 2012, 10:40 AM   #4
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Quote:
Originally Posted by l0uismustdie View Post
Hmm...this may be the case. I am running Snow Leopard though.
Code:
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
robvas is offline   0 Reply With Quote
Old Jan 15, 2012, 11:35 AM   #5
Mr. Retrofire
macrumors 601
 
Mr. Retrofire's Avatar
 
Join Date: Mar 2010
Location: www.emiliana.cl
Quote:
Originally Posted by l0uismustdie View Post
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/sour...vs-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.

:-)
__________________

“Only the dead have seen the end of the war.”
-- Plato --
Mr. Retrofire is offline   0 Reply With Quote
Old Jan 15, 2012, 01:30 PM   #6
Sander
macrumors 6502
 
Join Date: Apr 2008
It looks like you have to allocate the buffer for the line yourself. So change your code to

Code:
#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.
Sander is offline   0 Reply With Quote
Old Jan 15, 2012, 02:55 PM   #7
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by l0uismustdie View Post
Code:
#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.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
GCC compiled program not working zt0ne Mac Programming 24 Feb 5, 2014 01:28 PM
Where is my gcc and g++ compiler sysProgrammer Mac Programming 10 Nov 24, 2013 06:47 PM
Unable to update App Stores and unable download and install apps Firafire iPhone Tips, Help and Troubleshooting 0 Aug 11, 2013 09:03 PM
Can I compile and run the debugger on new code with an expired developer program? unphased iPhone and iPod touch Apps 0 May 29, 2013 09:30 PM
gcc doesn't work Mister V Mac Programming 6 Jan 6, 2013 11:04 AM

Forum Jump

All times are GMT -5. The time now is 06:50 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC