커널 모듈 프로그래밍 컴파일 방법좀 알려주세요 ㅠㅠ

hann2k의 이미지

게시판을 다 뒤졌는데 제 경우가 하나도 없군요 정말 괴롭습니다. ㅠㅠ

지금 제 환경은

한컴리눅스 4.0 을 기반으로 커널을 2.6.14-5 로 업글했습니다.

다음의 코드를 게시판에 조언 주신대로 컴파일 해보았습니다

#include <linux/moduleparam.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include "hello-c.h"

#if CONFIG_MODVERIONS==1

#define MODVERSIONS
#include <linux/modversions.h>

#endif

static  int __init init_module( void )
{
        printk("<KERNEL> Hello World.\n");

        /*
         * 리턴값이 0이 아닌것은 init_module이 실패한것을 의미한다
         * 따라서 모듈은 로드되지 못한다.
         */
        return 0;
}

static  void __exit cleanup_module( void )
{
        printk(KERN_ALERT "Goodbye world L.\n");
}

MODULE_LICENSE("GPL");

컴파일 방법 1.

TARGET  := hello-c
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}

CC := gcc

${TARGET}.o: ${TARGET}.c /usr/include/linux/version.h

.PHONY: clean

clean:
        rm -rf ${TARGET}.o

이 경우 hello-c.o 파일은 만들어 집니다만 insmod 를 사용하게 되면
[root@localhost test]# insmod hello-c.o
insmod: error inserting 'hello-c.o': -1 Invalid module format
라는 메시지가 나오구요

컴파일 방법 2.

obj-m   := hello-c.o
KDIR    := linux-2.6.14.5

default:
        $(MAKE) -C $(KDIR) SUBDIR=$(PWD) modules

clean:
        rm -rf *.ko
        rm -rf *.mod.*
        rm -rf .*.cmd
        rm -rf *.o

로 컴파일을 하게 되면 이런 메시지가 나옵니다.

$ make
make -C linux-2.6.14.5 SUBDIR=/home/hann2k/test modules
make[1]: Entering directory `/home/hann2k/test/linux-2.6.14.5'
  CHK     include/linux/version.h
  Building modules, stage 2.
  MODPOST
make[1]: Leaving directory `/home/hann2k/test/linux-2.6.14.5'
$

.ko 파일은 전혀 만들어지지 않는군요 괴롭습니다 ㅠㅠ
고수님들의 조언 부탁드립니다.[/code]

bitneer의 이미지

컴파일 방법 2. 의 Makefile에서 KDIR 부분을 다음과 같이 변경하고 PWD를 다음과 같이 추가 한 후 컴파일 해보세요.

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

hann2k의 이미지

bitneer wrote:
컴파일 방법 2. 의 Makefile에서 KDIR 부분을 다음과 같이 변경하고 PWD를 다음과 같이 추가 한 후 컴파일 해보세요.

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

아 어렵네요. 말씀하신 KDIR은 결국 /usr/src/linux-2.6.14.5를 링크하는거고요. 제가 썼던건 커널 소스 디렉토리를 직접 지정해준 거였거든요. /usr/src에 있는 소스는 루트로 압축풀고 커널 컴파일 했던거라서 root권한 파일들이 있어서 유저 권한으로는 컴파일 안되더라고요.

또 PWD는 $PWD로 사용할경우 현재 디렉토리를 나타내는거라서 굳이 위와 같이 지정하지 않더라도 SUBDIR=$(PWD) 했을때 현재디렉토리로 정상 동작하네요.

답변주셔서 감사합니다. 근데 동작이 안되니 괴롭군요 ㅠㅠ

hann2k의 이미지

쩝.. 성공했습니다. 신기하네요.

Makefile을 아래와 같이 만들었습니다

obj-m	+= hello-c.o

all:
	make -C ./linux M=$(PWD) modules

clean:
	make -C ./linux M=$(PWD) clean

그 전에도 컴파일할때 SUBDIR을 현재디렉토리로 지정하는듯 했는데 아무리 봐도 지금 컴파일할 소스 위치를 지정하는거 같았습니다. 근데 이상하게 리눅스 커널 소스로 지정된 -C 디렉토리로 가서는 거기있는거만 열심히 컴파일 하길래...
안되는 영어 뒤져서 찾았습니다.

http://www.tldp.org/LDP/lkmpg/2.6/html/x181.html

여기 가시면 2.6커널 모듈 프로그래밍 보실 수 있습니다.

SUBDIR을 사용하지 않고 M을 사용하네요 M이 뜻하는게 모듈 소스 위치를 말하는듯 합니다.

아뭏든 해결해서 기분 좋습니다. ^^/ 관심가져주신분들께 감사드려요~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.