未定义的引用`函数getline'在C [英] undefined reference to `getline' in c

查看:1768
本文介绍了未定义的引用`函数getline'在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C语言编程用函数getline,并试图在codeS由的http:/ /crasseux.com/books/ctutorial/getline.html

I am learning to use getline in C programming and tried the codes from http://crasseux.com/books/ctutorial/getline.html

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

int main(int atgc, char *argv[])
{
    int bytes_read = 1;
    int nbytes = 10;
    char *my_string;

    my_string = (char *)malloc(nbytes+1);

    puts("Please enter a line of text");

    bytes_read = getline(&my_string, &nbytes, stdin);

    if (bytes_read == -1)
    {
        puts ("ERROR!");
    }
    else
    {
        puts ("You typed:");
        puts (my_string);
    }

    return 0;
 }

然而,问题是,编译器保持这个返回错误:未定义引用函数getline
你能告诉我是什么问题?谢谢!

However, the problem is that the compiler keeps returning errors of this: undefined reference to 'getline'. Could you please tell me what the problem is? Thank you!

我使用的Win7 64位+ Eclipse的靛蓝+的MinGW

I am using Win7 64bit + Eclipse Indigo + MinGW

推荐答案

其他答案已经涵盖了大多数的这一点,但有几个问题。首先,<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html\"><$c$c>getline()是不是在C标准库,但它是一个POSIX 2008扩展。通常情况下,这将是可用与POSIX的兼容的编译器,作为宏_POSIX_C_SOURCE将用适当的值来定义。你可能有一个从旧的编译器之前函数getline()是标准化的,在这种情况下,这是一个GNU扩展,并且必须的#define _GNU_SOURCE 的#include&LT;文件stdio.h方式&gt; 来启用它,并且必须使用GNU兼容的编译器,如gcc

The other answers have covered most of this, but there are several problems. First, getline() is not in the C standard library, but is a POSIX 2008 extension. Normally, it will be available with a POSIX-compatible compiler, as the macros _POSIX_C_SOURCE will be defined with the appropriate values. You possibly have an older compiler from before getline() was standardized, in which case this is a GNU extension, and you must #define _GNU_SOURCE before #include <stdio.h> to enable it, and must be using a GNU-compatible compiler, such as gcc.

此外,的nbytes 的类型应该是为size_t ,而不是 INT 。在我的系统,至少,这些都是不同的大小,用为size_t 是更长的时间,并使用为int * 代替一个的为size_t * 可有严重的后果(也不能使用默认设置的gcc编译)。见,则对getline手册页(http://linux.die.net/man/3/getline)了解详情。

Additionally, nbytes should have type size_t, not int. On my system, at least, these are of different size, with size_t being longer, and using an int* instead of a size_t* can have grave consequences (and also doesn't compile with default gcc settings). See the getline manual page (http://linux.die.net/man/3/getline) for details.

随着这种变化做出,你的程序编译和我的系统上运行正常。

With that change made, your program compiles and runs fine on my system.

这篇关于未定义的引用`函数getline'在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆