I am reading FreeBSD Device Drivers book. The first example is hello.c. But, When I am compiling it, it says hello.c:1:10: fatal error: 'sys/param.h' file not found
我正在阅读FreeBSD设备驱动程序。第一个例子是hello.c。但是,当我编译它时,它会说hello。c:1:10:致命错误:“sys /参数。h的文件未找到
hello.c
安全
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
static int
hello_modevent(module_t mod __unused, int event, void *arg __unused)
{
int error = 0;
switch (event)
{
case MOD_LOAD:
uprintf("Hello, world\n");
case MOD_UNLOAD:
uprintf("GoodBye, cruel world");
default:
error = EOPNETSUPP;
}
return (error);
}
static moduledata_t hello_mod = {
"hello",
hello_modevent,
NULL
}
DECLARE_MODULE(hello, hello_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
Makefile
Makefile
KMOD= hello
SRCS= hello.c
.include <bsd.kmod.mk>
uname -a FreeBSD FreeBSD-1 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789: Fri Jan 17 01:46:25 UTC 2014 root@snap.freebsd.org:/usr/obj/usr/src/sys/GENERIC i386
uname—FreeBSD FreeBSD-1 10.0版本的FreeBSD 10.0-RELEASE #0 r260789: 5月17日(1月17日),root@snap.freebsd.org:/usr/obj/usr/src/sys/通用i386。
Thank you very much. Add:
非常感谢。添加:
After I type make, the problem is
Warning: Object directory not changed from original /usr/home/user/Workplaces/hello
cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc -I. -I@ -I@/contrib/altq -fno-common -mno-aes -mno-avx -mno-mmx -mno-sse -msoft-float -ffreestanding -fstack-protector -std=iso9899:1999 -Qunused-arguments -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -Wno-error-tautological-compare -Wno-error-empty-body -Wno-error-parentheses-equality -c hello.c
hello.c:1:10: fatal error: 'sys/param.h' file not found
#include <sys/param.h>
^
1 error generated.
*** Error code 1
2 个解决方案
#1
0
You need to have the kernel source installed if you want to build modules.
如果要构建模块,则需要安装内核源代码。
Additionally, your code has some errors:
此外,您的代码有一些错误:
> make
Warning: Object directory not changed from original /home/rsmith/tmp/src/foo
cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc -I. -I@ -I@/contrib/altq -fno-common -fno-omit-frame-pointer -mno-aes -mno-avx -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float -fno-asynchronous-unwind-tables -ffreestanding -fstack-protector -std=iso9899:1999 -Qunused-arguments -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -Wno-error-tautological-compare -Wno-error-empty-body -Wno-error-parentheses-equality -c hello.c
hello.c:17:22: error: use of undeclared identifier 'EOPNETSUPP'
error = EOPNETSUPP;
^
hello.c:27:2: error: expected ';' after top level declarator
}
^
;
2 errors generated.
*** [hello.o] Error code 1
The constant EOPNETSUPP
should be EOPNOTSUPP
from /usr/src/sys/sys/errno.h
.
不变的EOPNETSUPP应该是/usr/ src/sys/errno .h。
The code should look like this:
代码应该是这样的:
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/errno.h>
static int
hello_modevent(module_t mod __unused, int event, void *arg __unused)
{
int error = 0;
switch (event)
{
case MOD_LOAD:
uprintf("Hello, world\n");
case MOD_UNLOAD:
uprintf("GoodBye, cruel world");
default:
error = EOPNOTSUPP;
}
return (error);
}
static moduledata_t hello_mod = {
"hello",
hello_modevent,
NULL
};
DECLARE_MODULE(hello, hello_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
This compiles fine on my machine, FreeBSD 9.2-RELEASE amd64
这在我的机器上编译好了,FreeBSD 9.2-RELEASE amd64。
#2
0
I didn't change any thing except recompiling the kernel. The problem has been settled, though I do not know the reason. Thanks.
除了重新编译内核外,我没有改变任何东西。问题已经解决了,虽然我不知道原因。谢谢。
#1
0
You need to have the kernel source installed if you want to build modules.
如果要构建模块,则需要安装内核源代码。
Additionally, your code has some errors:
此外,您的代码有一些错误:
> make
Warning: Object directory not changed from original /home/rsmith/tmp/src/foo
cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc -I. -I@ -I@/contrib/altq -fno-common -fno-omit-frame-pointer -mno-aes -mno-avx -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float -fno-asynchronous-unwind-tables -ffreestanding -fstack-protector -std=iso9899:1999 -Qunused-arguments -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -Wno-error-tautological-compare -Wno-error-empty-body -Wno-error-parentheses-equality -c hello.c
hello.c:17:22: error: use of undeclared identifier 'EOPNETSUPP'
error = EOPNETSUPP;
^
hello.c:27:2: error: expected ';' after top level declarator
}
^
;
2 errors generated.
*** [hello.o] Error code 1
The constant EOPNETSUPP
should be EOPNOTSUPP
from /usr/src/sys/sys/errno.h
.
不变的EOPNETSUPP应该是/usr/ src/sys/errno .h。
The code should look like this:
代码应该是这样的:
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/errno.h>
static int
hello_modevent(module_t mod __unused, int event, void *arg __unused)
{
int error = 0;
switch (event)
{
case MOD_LOAD:
uprintf("Hello, world\n");
case MOD_UNLOAD:
uprintf("GoodBye, cruel world");
default:
error = EOPNOTSUPP;
}
return (error);
}
static moduledata_t hello_mod = {
"hello",
hello_modevent,
NULL
};
DECLARE_MODULE(hello, hello_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
This compiles fine on my machine, FreeBSD 9.2-RELEASE amd64
这在我的机器上编译好了,FreeBSD 9.2-RELEASE amd64。
#2
0
I didn't change any thing except recompiling the kernel. The problem has been settled, though I do not know the reason. Thanks.
除了重新编译内核外,我没有改变任何东西。问题已经解决了,虽然我不知道原因。谢谢。