如何将C编译输出文件(Linux内核模块)放置在与源文件不同的目录中';s(使用Makefile)
我看了又看,但无法编写正确的Makefile来产生我想要的结果 因此,我有一个如何将C编译输出文件(Linux内核模块)放置在与源文件不同的目录中';s(使用Makefile),c,linux,makefile,compilation,C,Linux,Makefile,Compilation,我看了又看,但无法编写正确的Makefile来产生我想要的结果 因此,我有一个simple.c文件。它模拟linux内核模块的加载和删除。位置:/path/to/dir/simple.c #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> /* This function is called when the module is loaded. */ int
simple.c
文件。它模拟linux内核模块的加载和删除。位置:/path/to/dir/simple.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
在终端上运行时:
cd path/to/dir/
make
所有内容都已正确编译(在同一目录path/to/dir/
中)
我想要的是:
simple.c
在/path/to/dir/src/
中的位置
Makefile
在/path/to/dir/
中的位置
输出在/path/to/dir/bin/
或/和/path/to/dir/obj/
中的位置
运行make
时,输出应在bin
,obj
目录中结束
Makefile(/lib/modules/$(shell uname-r)/build
)中有一些我不太理解的复杂之处。为了达到预期的结果,对Makefile
所做的所有更改都没有成功地出错
我该怎么做
编辑:
/lib/modules/$(shell uname-r)/build
中的Makefile
包含以下代码:
VERSION = 4
PATCHLEVEL = 4
SUBLEVEL = 162
EXTRAVERSION =
NAME = Blurry Fish Butt
# *DOCUMENTATION*
# To see a list of typical targets execute "make help"
# More info can be located in ./README
# Comments in this file are targeted only to the developer, do not
# expect to learn how to build the kernel reading this file.
# o Do not use make's built-in rules and variables
# (this increases performance and avoids hard-to-debug behaviour);
# o Look for make include files relative to root of kernel src
MAKEFLAGS += -rR --include-dir=$(CURDIR)
# Avoid funny character set dependencies
unexport LC_ALL
LC_COLLATE=C
LC_NUMERIC=C
export LC_COLLATE LC_NUMERIC
# Avoid interference with shell env settings
unexport GREP_OPTIONS
# We are using a recursive build, so we need to do a little thinking
# to get the ordering right.
#
# Most importantly: sub-Makefiles should only ever modify files in
# their own directory. If in some directory we have a dependency on
# a file in another dir (which doesn't happen often, but it's often
# unavoidable when linking the built-in.o targets which finally
# turn into vmlinux), we will call a sub make in that other dir, and
# after that we are sure that everything which is in that other dir
# is now up to date.
#
# The only cases where we need to modify files which have global
# effects are thus separated out and done before the recursive
# descending is started. They are now explicitly listed as the
# prepare rule.
# Beautify output
# ---------------------------------------------------------------------------
#
# Normally, we echo the whole command before executing it. By making
"/lib/modules/4.4.0-141-generic/build/Makefile" [readonly] 1650L, 57062C
我不知道我是否理解了这个问题。首先,我建议您在SRC文件夹中找到源代码。然后,选择要创建Makefile的目录 下一步是定义链接程序所需的代码集。不要忘记SRC文件夹的路径 现在是创建对象文件的时候了。在这一步中,选择选项-o[path\u obj]/[file\u name].o 最后一步是链接程序,不要忘记对象位于[path_obj]文件夹中 一个简单的例子是:
#path definitions
SRC_path = /path_to_src/
OBJ_path = /path_to_obj/
BIN_path = /path_to_bin/
#lists definitions
SRC = [file1].c [file2].c
OBJ = $(addsuffix .o, $(basename ${SRC}))
#Suffixes definitions
.suffixes:
.suffixes: .c .o
#Create objects
.c.o: gcc -I[include_files] -c $(SRC_path)$< -o $(OBJ_path)$@
#Link program
TAG: gcc $(addprefix $(OBJ_path), $(OBJ)) -o $(BIN_path)[program_name]
路径定义
SRC\u path=/path\u to\u SRC/
OBJ_path=/path_to_OBJ/
BIN\u path=/path\u to\u BIN/
#列出定义
SRC=[file1].c[file2].c
OBJ=$(addsuffix.o,$(basename${SRC}))
#后缀定义
.后缀:
.后缀:.c.o
#创建对象
.c.o:gcc-I[包括文件]-c$(SRC\u路径)$<-o$(OBJ\u路径)$@
#链接程序
标记:gcc$(addprefix$(OBJ_路径),$(OBJ))-o$(BIN_路径)[程序名称]
我希望您会发现它很有用在
make-
C的目标目录中需要有一个Makefile,即
/lib/modules/$(shell uname-r)/build`。你确定有一个吗?它看起来像什么?你的makefile只在别处调用另一个makefile。如果你想要不同的行为,你必须修改另一个makefile,或者依赖它。@joH1我编辑了这个问题,将makefile
的内容包含在/lib/modules中/$(shell uname-r)/build
。内核构建有自己的一组规则。要从中提取就绪模块,您可以尝试使INSTALL\u MOD\u PATH=modules\u INSTALL
。
#path definitions
SRC_path = /path_to_src/
OBJ_path = /path_to_obj/
BIN_path = /path_to_bin/
#lists definitions
SRC = [file1].c [file2].c
OBJ = $(addsuffix .o, $(basename ${SRC}))
#Suffixes definitions
.suffixes:
.suffixes: .c .o
#Create objects
.c.o: gcc -I[include_files] -c $(SRC_path)$< -o $(OBJ_path)$@
#Link program
TAG: gcc $(addprefix $(OBJ_path), $(OBJ)) -o $(BIN_path)[program_name]