1

I am trying to use getline in C with codeblocks and I am having trouble getting it to run on my machine. The code works on a server I have access too but I have limited wifi access so I need this to work on my machine. I am running windows 8.1 64 bit and codeblocks 13.12 with the gcc compiler.

here is the one of the three sections of code that uses getline, with some of the extra variables removed.

 #include <stdio.h>
 #include <stdlib.h> // For error exit()
 #include <string.h>


 char *cmd_buffer = NULL;
 size_t cmd_buffer_len = 0, bytes_read = 0;
 size_t words_read; // number of items read by sscanf call
 bytes_read = getline(&cmd_buffer, &cmd_buffer_len, stdin);

 if (bytes_read == -1) {
        done = 1; // Hit end of file
 }

the error is very simply:

undefined reference to 'getline'

How can I get this to work?

EDIT I added the headers. I also want to mention that I saw a few posts on this site that did not work for me.

bs7280
  • 1,074
  • 3
  • 18
  • 32

1 Answers1

5

Perhaps this work.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

ssize_t getdelim(char **linep, size_t *n, int delim, FILE *fp){
    int ch;
    size_t i = 0;
    if(!linep || !n || !fp){
        errno = EINVAL;
        return -1;
    }
    if(*linep == NULL){
        if(NULL==(*linep = malloc(*n=128))){
            *n = 0;
            errno = ENOMEM;
            return -1;
        }
    }
    while((ch = fgetc(fp)) != EOF){
        if(i + 1 >= *n){
            char *temp = realloc(*linep, *n + 128);
            if(!temp){
                errno = ENOMEM;
                return -1;
            }
            *n += 128;
            *linep = temp;
        }
        (*linep)[i++] = ch;
        if(ch == delim)
            break;
    }
    (*linep)[i] = '\0';
    return !i && ch == EOF ? -1 : i;
}
ssize_t getline(char **linep, size_t *n, FILE *fp){
    return getdelim(linep, n, '\n', fp);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • you just saved me! you must be a wizard! thank you so much. I never expected this to work considering my luck today! you are going to save my final project – bs7280 Dec 09 '14 at 01:07
  • `getline` definitely shouldn't free the memory if reallocation fails. Beside that it doesn't look like OP wanted to re-implement these functions. – mafso Dec 09 '14 at 20:10
  • @mafso Well, It is to update on success. I had misunderstood. also it is does not help when you answer that does not exist in the windows environment. – BLUEPIXY Dec 09 '14 at 20:21
  • I'm not familiar with Windows (and what standard library is used with MinGW, for example), but OP didn't mention any warnings (for the implicit function declaration), so it seems to be declared. Which in turn means, the header included may not match the standard library linked to. If that's the case, this should be fixed instead of re-implementing functions. – mafso Dec 09 '14 at 21:04
  • Retracted the downvote of course, the inability for the caller to know if the memory is freed or not is now removed (which was quite a severe issue). – mafso Dec 09 '14 at 21:04
  • 1
    1) Do not think you need `ch == EOF` test in `return !i && ch == EOF ? -1 : i;`. Suggest simplification: `return !i ? -1 : i;` 2) `*n += 128` should happen _afterwards_. `char *temp = realloc(*linep, *n + 128); if(!temp){ return -1; } *linep = temp; *n += 128`. 3) Suggest `ENOMEM` on malloc/realloc failure. – chux - Reinstate Monica Dec 09 '14 at 21:19
  • @chux thanks a lot. I'm really grateful to your help. – BLUEPIXY Dec 09 '14 at 21:50
  • 1) `ch == EOF` : Although there is certainly redundant, I leave this for if the version that does not include the delimiter became i want. – BLUEPIXY Dec 09 '14 at 22:01