简体   繁体   中英

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"

Such, include/linux/socket.h in the my kernel source was changed, as seen here(as well as for 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. 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.

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. 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.

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.

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

FYI, compiled using 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. 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, ...).

This page has some more information about this: 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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