【问题标题】:Why coudn't I use function grantpt in Linux?为什么我不能在 Linux 中使用函数 grantpt?
【发布时间】:2015-01-07 12:38:50
【问题描述】:

我想在我的程序中使用函数grantpt。我包含了它的头文件<stdlib.h>,但编译器仍然给我warning: implicit declaration of grantpt,这意味着它在stdlib.h 中找不到声明。我grep头文件,找到声明:

stdlib.h:920:extern int grantpt (int __fd) __THROW;  

而我的glibc版本是2.17,官方手册上说是2.1以后才包含这个功能。

这是我的测试程序:

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

typedef unsigned long u_l;  

int main(){
    int errno = grantpt(1);
    printf("errno = %d\n", errno);
    return 0;
}

非常感谢!

【问题讨论】:

  • 如果您阅读the manual page,您会发现需要定义宏_XOPEN_SOURCE,但显然不需要。

标签: c shared-libraries


【解决方案1】:
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
//#include<sys/poll.h>

typedef unsigned long u_l;

int main()
{
    int errno = grantpt(1);

    printf("errno = %d\n", errno);

    return 0;
}

此代码在没有警告的情况下编译

Man page明确表示需要在包含stdlib.h之前定义_XOPEN_SOURCE以访问grantpt()

【讨论】:

  • 现在一切顺利!非常感谢!
  • 谢谢你在stackoverflow正在接受答案并支持他们
  • Ubuntu 20.04:#define _GNU_SOURCE 而不是 _XOPEN_SOURCE 为我工作
  • 这不是您检索 errno 的方式,granpt(3) 信号返回 -1,它设置它不返回它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多