【问题标题】:Using gcc to compile userspace application using linux kernel headers使用 gcc 使用 linux 内核头文件编译用户空间应用程序
【发布时间】:2017-09-07 13:01:31
【问题描述】:

我有一个非常简单的 c 程序,我想使用 gcc 编译,从 linux 内核头文件导入。

#include <stdio.h>
#include <stdlib.h>
#include <linux/random.h>

int main(){
    int rand;
    get_random_bytes(&rand,sizeof(rand));
    printf("%d",rand);
    return 0;
}

我尝试使用以下命令编译此程序:

gcc rand.c -D__KERNEL__ -isystem /lib/modules/`uname -r`/build/include

但是我得到了一堆错误(如下)。我错过了什么?:

/usr/src/kernels/4.9.8-201.fc25.x86_64/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
 #include <asm/linkage.h>

【问题讨论】:

  • 你为什么放-D__KERNEL__ -isystem,如果你把它们拿出来会发生什么?
  • @tripleee 因为我在谷歌搜索后发现了这种模式.....gcc rand.c 给出了rand.c:(.text+0x1a): undefined reference to get_random_bytes'
  • 好的,谢谢,这是有道理的。我只想编写一个用户空间程序来删除我正在处理的内核驱动程序的某些部分。看起来这种方法行不通。

标签: c linux gcc


【解决方案1】:

从一些快速的谷歌搜索来看,get_random_bytes 似乎是一个只能在内核中使用的私有函数。

您尝试改用getrandom 怎么样?这是getrandom的文档:

http://man7.org/linux/man-pages/man2/getrandom.2.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    相关资源
    最近更新 更多