codeblocks and C undefined reference to getline

[亡魂溺海] 提交于 2019-12-10 12:08:24

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/27369580/codeblocks-and-c-undefined-reference-to-getline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!