各種プログラム、Makefile サンプル。
◆シェルサンプル
shell/test.sh
◆Makefileサンプル
csrc/Makefile
csrc/main.c
◆Linux LKM(ローダブルカーネルモジュール)サンプル
kbuild/Makefile (KDIR は場合によっては任意に変更)
kbuild/test/Makefile
kbuild/test/test_base.c
◆シェルサンプル
shell/test.sh
#!/bin/bash
# ./test.sh 1 "2 3" 4
TMPFILE=/tmp/test
init() {
if [ -f ${TMPFILE} ]; then
rm -f ${TMPFILE}
elif [ -e ${TMPFILE} ]; then
exit 1
fi
touch ${TMPFILE}
if [ $? -ne 0 ]; then
exit 1
fi
}
main() {
local arg
local i=0
for arg in "$@"; do
echo ${arg} >> ${TMPFILE}
list[${i}]=${arg}
i=$((${i} + 1))
done
i=0
while [ ${i} -lt ${#list[@]} ]; do
echo ${list[${i}]}
i=$((${i} + 1))
done
list2=(`cat ${TMPFILE}`)
for i in "${list[@]}"; do
echo ${i}
done
while read line; do
echo ${line}
done < ${TMPFILE}
}
init
main "$@"
◆Makefileサンプル
csrc/Makefile
CC=gcc上記サンプル↓
CFLAGS ?= -O2 -Wall
LDFLAGS ?=
OBJS=main.o
TARGET=test
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
debug:
$(MAKE) -C . CFLAGS="$(CFLAGS) -DDEBUG"
.c.o:
$(CC) -c $< $(CFLAGS)
clean:
$(RM) $(TARGET) $(OBJS)
$ curl -O http://sstea.blog.jp/raspi/csrc/Makefile
csrc/main.c
#include <stdio.h>
#ifdef DEBUG
#define DEBUG(...) printf("<DEBUG> " __VA_ARGS__)
#else /* !DEBUG */
#define DEBUG(...) do { } while (0)
#endif /* !DEBUG */
int main(int argc, char **argv)
{
DEBUG("hello\n");
return 0;
}
◆Linux LKM(ローダブルカーネルモジュール)サンプル
kbuild/Makefile (KDIR は場合によっては任意に変更)
obj-y := test/上記サンプル↓
KDIR ?= /lib/module/$(shell uname -r)/build
PWD := $(shell pwd)
EXTRA_CFLAGS =
modules:
make -C $(KDIR) M=$(PWD) V=1 modules
clean:
make -C $(KDIR) M=$(PWD) V=1 clean
$ curl -O http://sstea.blog.jp/raspi/kbuild/Makefile
kbuild/test/Makefile
obj-m:= test.otest-objs に.oファイルを複数指定することで、複数ファイルから1つのローダブルカーネルモジュール(.koファイル)を作成可能。
test-objs := test_base.o
kbuild/test/test_base.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
static int __init test_init(void)
{
printk("test_init\n");
return 0;
}
module_init(test_init);
static void __exit test_exit(void)
{
printk("test_exit\n");
}
module_exit(test_exit);