简体   繁体   English

针对自定义Linux内核编译简单C程序时出现问题

[英]Problems Compiling simple C program against custom Linux Kernel

I recently compiled a custom kernel which defines a new address family/protocol family called "AF_CUSTOM" 我最近编译了一个自定义内核,该内核定义了一个称为“ AF_CUSTOM”的新地址族/协议族。

Such, include/linux/socket.h in the my kernel source was changed, as seen here(as well as for PF_CUSTOM): 这样,我的内核源代码中的include / linux / socket.h已更改,如下所示(以及PF_CUSTOM):

#define AF_NFC      39  /* NFC sockets          */
#define AF_CUSTOM       40  /* Custom sockets           */
#define AF_MAX      41  /* For now.. */

I plan to implement AF_CUSTOM, but as quick sanity check, I decided to modify a typical sample c socket program and see if replacing "socket(AF_INET, SOCK_STREAM, 0)" with "socket(AF_CUSTOM, SOCK_STREAM, 0)" would compile, but it did not. 我计划实现AF_CUSTOM,但是为了进行快速的健全性检查,我决定修改一个典型的示例c套接字程序,并查看是否可以将“ socket(AF_INET,SOCK_STREAM,0)”替换为“ socket(AF_CUSTOM,SOCK_STREAM,0)”,但事实并非如此。 I got the following error when compiling with gcc: 'AF_CUSTOM' undeclared I assumed that it would at least compile, because AF_CUSTOM should be defined in the current kernel. 使用gcc进行编译时出现以下错误: 'AF_CUSTOM' undeclared我认为它将至少进行编译,因为AF_CUSTOM应该在当前内核中定义。

The problem, as I see it, is that gcc is using the default kernel headers to resolve "#include <sys/socket.h>" and not the headers that correspond to the custom kernel that is currently running. 如我所见,问题是gcc使用默认内核头文件解析“ #include <sys / socket.h>”,而不是与当前正在运行的自定义内核相对应的头文件。 I tried using both -I and -isystem options on gcc to direct it to the path that Ubuntu seems to place the kernel headers for the other kernels, as they seemed relevant from my Google research, but they didn't help. 我尝试在gcc上同时使用-I和-isystem选项将其定向到Ubuntu似乎放置其他内核的内核头的路径,因为它们与我的Google研究相关,但没有帮助。

My question is: How can I compile a C program against the headers for the currently running custom kernel as opposed to the default kernel headers. 我的问题是:如何针对当前运行的自定义内核的标头而不是默认内核标头编译C程序。

I tried this: gcc -isystem /usr/src/linux-headers-3.8.8-custom.5/ sendOnCustom.c -o sendOnCustom 我试过了: gcc -isystem /usr/src/linux-headers-3.8.8-custom.5/ sendOnCustom.c -o sendOnCustom

FYI, compiled using make-kpkg. 仅供参考,使用make-kpkg进行编译。 Also, this is my first question, I hope it is understandable. 另外,这是我的第一个问题,我希望这是可以理解的。

It's common practice on Linux systems to use a "sanitized" copy of the Linux kernel headers to build software against. 在Linux系统上,通常的做法是使用经过“消毒”的Linux内核标头副本来构建软件。 Probably, GCC is looking for the headers not in /usr/src/linux, but in some other location (which is probably distro-specific -- on my machine, which is running Gentoo, these headers are placed in /usr/include/asm, /usr/include/linux, ...). GCC可能不在/ usr / src / linux中寻找标头,而是在其他位置(可能是发行版特定的)在我的运行Gentoo的机器上,这些标头放在/ usr / include /中。 asm,/ usr / include / linux,...)。

This page has some more information about this: http://headers.cross-lfs.org/ 本页包含有关此内容的更多信息: http : //headers.cross-lfs.org/

My guess is that you need to make very sure that when you compile your include path has /usr/src/linux in it (or wherever your modified header is), and that line needs to at least come before the other sanitized headers, if you can't omit them entirely. 我的猜测是,您需要确保在编译时,include路径中包含/ usr / src / linux(或修改后的标头所在的位置),并且如果需要,该行至少应位于其他经过清理的标头之前您不能完全省略它们。

Another thing to note is that, from your post, it seems like you expect any symbols defined for the current kernel to be defined everywhere - this is not the case. 还要注意的另一件事是,从您的帖子中看来,您似乎希望在当前位置到处定义为当前内核定义的任何符号-并非如此。 Whenever you compile software, the defines the compiler sees have nothing to do with the running kernel. 每当您编译软件时,编译器就会看到与正在运行的内核无关的定义。 The compiler only knows how to search its include path to try to find the headers you specify, and examines those. 编译器仅知道如何搜索其包含路径以尝试查找您指定的标头,然后进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM