当前位置:网站首页>通用的 kernel和 userspace Makefile

通用的 kernel和 userspace Makefile

2022-08-11 05:23:00 丶随心

User space的Makefile, 保存为一个Makefile文件

TARGET:=main
SOURCE_FILES:=$(wildcard *.c)
INCLUDE_FILES:=$(wildcard *.h)
INCLUDE_FILES+=$(wildcard *.h)
 
OBJ:=$(patsubst %.c, %.o, $(SOURCE_FILES))
 
C_FLAGS:= -g -Wall -O2 \
          -fsanitize=address
 
LINK_MODULES:=-pthread
 
LDFLAGS := -L../out/lib/
#LINK_MODULES    += -lmodulename
 
$(TARGET):$(OBJ)
	gcc $(C_FLAGS) $(LINK_MODULES) $(LDFLAGS) -o $(TARGET) $(OBJ)
 
%.o:%.c $(INCLUDE_FILES)
	gcc $(C_FLAGS) -c -o [email protected] $<
 
.PHONY:clean
clean:
	rm -f $(TARGET) $(OBJ)

Kernel space的Makefile, 保存为一个Makefile文件

CONFIG_MODULE_SIG=n

obj-m := icdev.o

CURRENT_PATH:=$(shell pwd)
#the current kernel version number
LINUX_KERNEL:=$(shell uname -r)
#the absolute path
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
 
#complie object
all:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules   ######
#clean
clean:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean     ######

带目录设置的通用C++ Makefile

######### Makefile COMMON ########
#c or c++
EXTENSION =c
#gcc or g++
CC = gcc
#exe name
TARGET = mpls_dump
#source-file-direction space add-more-as: src src1 src2
SUBDIR =file util .

#.o file direction
OBJ_DIR = out

#(including .h file direction)
#CPPFLAGS += -I ./inc
CPPFLAGS = -I . 


# link flags
LDFLAGS += -pthread

# C flags
CFLAGS += -g -Wall -O2 \
          -fsanitize=address \
		  -DHAVE_CONFIG_H 

#not change lines below
ALL_SOURCES=$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.$(EXTENSION)))
SOURCE_TO_OBJECT = ./$(OBJ_DIR)/$(subst .$(EXTENSION),.o,$(notdir $(1)))
ALL_OBJECTS=$(foreach src,$(ALL_SOURCES),$(call SOURCE_TO_OBJECT,$(src)))

define CREATE_OBJECT_TARGET
$(call SOURCE_TO_OBJECT,$(1)) : $(1)
	if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi;\
	$(CC) -c  $(CFLAGS) $(CPPFLAGS) -o [email protected] $$^
endef

$(TARGET):$(ALL_OBJECTS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o [email protected] $^
	mv $(TARGET) $(OBJ_DIR)

$(foreach src,$(ALL_SOURCES),$(eval $(call CREATE_OBJECT_TARGET,$(src))))

all: $(TARGET)

.PHONY:clean
clean:
	rm -rf $(OBJ_DIR) $(TARGET)


 

原网站

版权声明
本文为[丶随心]所创,转载请带上原文链接,感谢
https://blog.csdn.net/suixin______/article/details/117130033