gpt4 book ai didi

c - 使用 gcc 使用 linux 内核头文件编译用户空间应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:09 27 4
gpt4 key购买 nike

我有一个非常简单的 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>

最佳答案

从 Google 的一些快速搜索中,get_random_bytes 似乎是一个只能在内核中使用的私有(private)函数。

您尝试使用 getrandom 怎么样?以下是 getrandom 的文档:

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

关于c - 使用 gcc 使用 linux 内核头文件编译用户空间应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46087455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com