将内核模块构建到特定目录中 [英] Build kernel module into a specific directory

查看:92
本文介绍了将内核模块构建到特定目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以设置输出目录,以在我的makefile中创建内核模块?

is there a way to set a output-directory for making kernel-modules inside my makefile?

我想保持源代码目录中所有源文件的清洁.

I want to keep my source-direcory clean from the build-files.

推荐答案

KBUILD_OUTPUTO=对我不起作用,并且在外部构建时找不到内核标头. 我的解决方案是将源文件符号链接到bin目录中,并在bin目录中动态生成一个新的MakeFile.由于可以随时重新创建动态Makefile,因此可以轻松清除所有构建文件.

KBUILD_OUTPUT and O= did not work for me and were failing to find the kernel headers when building externally. My solution is to symlink the source files into the bin directory, and dynamically generate a new MakeFile in the bin directory. This allows all build files to be cleaned up easily since the dynamic Makefile can always just be recreated.

INCLUDE=include
SOURCE=src
TARGET=mymodule
OUTPUT=bin
EXPORT=package

SOURCES=$(wildcard $(SOURCE)/*.c)

# Depends on bin/include bin/*.c and bin/Makefile
all: $(OUTPUT)/$(INCLUDE) $(subst $(SOURCE),$(OUTPUT),$(SOURCES)) $(OUTPUT)/Makefile
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD)/$(OUTPUT) modules

# Create a symlink from src to bin
$(OUTPUT)/%: $(SOURCE)/%
    ln -s ../$< $@

# Generate a Makefile with the needed obj-m and mymodule-objs set
$(OUTPUT)/Makefile:
    echo "obj-m += $(TARGET).o\n$(TARGET)-objs := $(subst $(TARGET).o,, $(subst .c,.o,$(subst $(SOURCE)/,,$(SOURCES))))" > $@

clean:
    rm -rf $(OUTPUT)
    mkdir $(OUTPUT)

这篇关于将内核模块构建到特定目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆