codeblocks和C对getline的未定义引用

时间:2022-11-21 05:30:36

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.

我尝试在C中使用getline和代码库,但是我在让它在我的机器上运行时遇到了麻烦。代码在我可以访问的服务器上工作,但是我的wifi有限,所以我需要它在我的机器上工作。我正在运行windows 8.1 64位,并使用gcc编译器对13.12进行编码。

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

下面是使用getline的代码的三个部分之一,去掉了一些额外的变量。

 #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 个解决方案

#1


3  

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

#1


3  

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