問題描述
如何在內核源文件中包含 math.h #include <math.h>? (How to include math.h #include <math.h> on kernel source file?)
I am trying to include math.h in my Linux kernel module. If I use,
#include '/usr/include/math.h'
It give me theses errors:
error: features.h: No such file or directory
error: bits/huge_val.h: No such file or directory
error: bits/mathdef.h: No such file or directory
error: bits/mathcalls.h: No such file or directory
Why is this?
參考解法
方法 1:
You cannot use the C library in a kernel module, this is even more true for the math library part.
方法 2:
You can't include a userspace C module in kernel space. Also are you sure that you want to be doing this? This thread may help http://kerneltrap.org/node/16570. You can do math functions inside the kernel, just search around on http://lxr.linux.no/ for the function you need.
方法 3:
Standard libraries are not available in the kernel. This includes libc, libm, etc. Although some of the functions in those libraries are implemented in kernel space, some are not. Without knowing what you're trying to call, it's impossible to say for sure whether or not you should be doing what you're trying to do in kernel space.
I should further note that the kernel does NOT have access to the FPU. This is to save time when switching tasks (since saving the FPU registers would add unnecessary overhead when performing context switches). You can get access to the FPU from kernel space if you really want it, but you need to be really careful not to trash the user space's FPU registers when doing so.
Edit: This summarizes the caveat about the FPU much better than I did.
方法 4:
Floating point operations is not supported in the kernel. This is because when switching from kernel context to user context, registers must be saved. If the kernel would make use of floating point, then also the floating point registers would have to be saved also, which would cause bad performance for each context switch. So because floating point is very rarely needed, especially in the kernel it is not supported.
If you really have to:
- maybe you could compile your own kernel with floating point support
- you could block context switch within your floating point operations
- the best would be to use fixed point arithmetics.
方法 5:
AFAIK kernel space is separated from user space, and so should the source code. /usr/include is for general programming.
(by Madni、David Cournapeau、Sweeney、FreeMemory、codymanix、Andrei Tanasescu)