คุณมี project ที่บางครั้งคุณไม่ต้องการที่จะเปิดเผย function บางอย่างบน Internet คือหมายความว่าไฟล์ที่คุณไม่ต้องการ

เปิดเผยให้ผู้ใช้งานนำไปเปลียนแปลงหรือกระทำการใดๆ คุณก็สามารถที่จะใช้วิธีการสร้างไฟล์ที่เรียกว่า LIB เพื่อการนำำไปเรียกใช้


ยกตัวอย่าง เป็น full code ที่สามารถเปิดและทำการแก้ไขได้ คือ files นั้นถูกเปิดหมด ผู้ที่ download สามารถทำการแก้ไขได้ทุกอย่าง ลองดาวโหลดมา

เพื่อเปรียบเทียบในขั้นตอนที่ 1 และขั้นตอนที่ 2


ขั้นตอนที่ 1 ยกตัวอย่างว่า ในโปรแกรมบาง Files ที่ไม่ต้องการที่จะ Open source แต่จะต้องสามารถให้มีการเรียกใช้ได้ จึงต้องมีวิธีการที่จะเปลี่ยน

รูปแบบของไฟล์ที่ต้องการให้เป็นรูปแบบอื่น ซึ่งภาษาเรียกของนักเขียนโปรแกรมเรียกว่า การสร้าง LIB วิธีการนี้จะทำให้ไฟล์นั้นไม่สามารถถูกเปิด

ออกมาเพื่อทำการแก้ไข หรือ เปิดมาเพื่อมองเห็นส่วนต่างๆ ที่อยู่ในไฟล์ ผมขอยกตัวอย่างไฟล์ LCD.C ซึ่งเป็นไฟล์ที่ไม่ต้องการเปิด จึงต้องมีการ

เปลียนไฟล์นี้ให้เป็น LIB วิธีการคือ ปกติแล้วการ make file output ที่ได้จะเป็น HEX เพื่อนำไปโปรแกรมลงตัว MCU วิํธีการสร้าง LIB นี้

จะต้องมีการกำหนด Output ของไฟล์ของการคอมไพล์ ซึ่งหลังจากที่คอมไำพล์แล้ว จะได้ไฟล์ชื่อ "lcdlib.lib" จากตัวอย่างเป็นการสร้างจากไฟล์ lcd.c

แต่เราสามารถมีไฟล์อื่นมาสร้างร่วมได้ ซึ่งเอาจะต้องมีการกำหนดไฟล์ ที่นำมาสร้างร่วมใน makefile ลองดูในตัวอย่าง

# Tools and directories
CC = avr-gcc
AS = avr-gcc -x assembler-with-cpp
RM = rm -f
RN = mv
BIN = avr-objcopy
INCDIR = .
LIBDIR = $(AVR)\avr\lib
SHELL = $(AVR)\bin\sh.exe
FORMAT = srec

############################################################################
# library (.a) file creation command
AR= avr-ar rc

############################################################################
# CPU type
MCU = atmega128

# Target
TRG = lcdlib <---- create lib file name here

# C-source files
LIBSRC = lcd.c add any files here xxx.c yyy.c

# Compiler flags
CPFLAGS = -g -Os -fregmove -fforce-addr -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

# Assembler flags
ASFLAGS = -Wa,-gstabs

# Linker flags
LDFLAGS = -Wl,--defsym,__init_wdtcr__=0x07,--defsym,__init_mcucr__=0x80,-Map=$(TRG).map,--cref

#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB = $(PRINTF_LIB_FLOAT)

#--------------------------------------------------------------------------
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt

# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB = $(SCANF_LIB_FLOAT)

MATH_LIB = -lm
############################################################################
############################################################################

#define all project specific object files
LIBOBJ = $(LIBSRC:.c=.o)
CPFLAGS += -mmcu=$(MCU)
ASFLAGS += -mmcu=$(MCU)
LDFLAGS += -mmcu=$(MCU)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)

#compile: instructions to create assembler and/or object files from C source
%o : %c
$(CC) -c $(CPFLAGS) -I$(INCDIR) $< -o $@

%s : %c
$(CC) -S $(CPFLAGS) -I$(INCDIR) $< -o $@

all: $(TRG).lib <---- Target Output
$(TRG).lib : $(LIBOBJ)
$(RM) $(TRG).lib
$(AR) $(TRG).lib $(LIBOBJ)
avr-size $(TRG).lib

#make instruction to delete created files
clean:
$(RM) $(LIBOBJ)
$(RM) $(TRG).map
$(RM) $(TRG).elf
$(RM) $(TRG).obj
$(RM) $(TRG).lib
$(RM) *.bak
$(RM) *.lst
$(RM) ../*.lst
$(RM) *.?_sym

############################################################################
############################################################################
######## dependecies, add any dependencies you need here ###################

lcd.o : lcd.c lcd.h makefile

you can add more files here

xxx.o xxx.c xxx.h makefile

yyy.o yyy.c yyy.h makefile


 


ขั้นตอนที่ 2 หลังจากที่ผ่านขั้นตอนที่ 1 ให้ทำการ copy "lcdlib.lib" มาที่ Folder นี้ แล้วให้สังเกตุว่าไม่มี file "lcd.c" นั้นหมายความว่า "lcd.c" จะถูก

สร้างมาเป็น files "lcdlib.lib" แทน แล้วนำมาเรียกใช้ ทางผู้ที่ download ไปใช้งานไม่สามารถเข้าไปทำการแก้ไข files "lcdlib.lib" ได้และการ make file

ในขั้นตอนนี้ สามารถมี file อื่นๆ เพิ่มเติมได้ โดยไฟล์เหล่านี้ เป็นการ Open Code ที่นำมา make ร่วมการใช้งานใน ขั้นตอนที่ 1 และขั้นตอนที่ 2

ให้ดูวิธีการที่ file "makefile"

# Tools and directories
CC = avr-gcc
AS = avr-gcc -x assembler-with-cpp
RM = rm -f
RN = mv
BIN = avr-objcopy
INCDIR = .
LIBDIR = $(AVR)\avr\lib
SHELL = $(AVR)\bin\sh.exe
FORMAT = srec


# CPU type
MCU = atmega128

# Target
TRG = main <--- Output for copy to flash MCU Output = main.hex

# C-source files
SRC = serial.c printf.c $(TRG).c <---- Add any files here jnut.c inex.c

# Libraries
LIB = lcdlib.lib <--- call Lib from step here

# Compiler flags
CPFLAGS = -g -Os -fregmove -fforce-addr -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst) -mcall-prologues -I$LIB

# Assembler flags
ASFLAGS = -Wa,-gstabs

# Linker flags
LDFLAGS = -Wl,--defsym,__init_wdtcr__=0x07,--defsym,__init_mcucr__=0x80,-Map=$(TRG).map,--cref

#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
PRINTF_LIB = $(PRINTF_LIB_FLOAT)

#--------------------------------------------------------------------------
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt

# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
SCANF_LIB = $(SCANF_LIB_FLOAT)

MATH_LIB = -lm

############################################################################################
############################################################################################

#define all project specific object files
OBJ = $(ASRC:.s=.o) $(SRC:.c=.o)
CPFLAGS += -mmcu=$(MCU)
ASFLAGS += -mmcu=$(MCU)
LDFLAGS += -mmcu=$(MCU)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)


#this defines the aims of the make process
all: $(TRG).elf $(TRG).hex
<---- Target Output

#compile: instructions to create assembler and/or object files from C source
%o : %c
$(CC) -c $(CPFLAGS) -I$(INCDIR) $< -o $@

%s : %c
$(CC) -S $(CPFLAGS) -I$(INCDIR) $< -o $@

#assemble: instructions to create object file from assembler files
%o : %s
$(AS) -c $(ASFLAGS) -I$(INCDIR) $< -o $@

#link: instructions to create elf output file from object files
%elf: $(OBJ)
$(CC) $(OBJ) $(LIB) $(LDFLAGS) -o $@

#create avrobj file from elf output file
%obj: %elf
$(BIN) -O avrobj $< $@

#create bin (ihex, srec) file from elf output file
%rom: %elf
$(BIN) -O $(FORMAT) $< $@

%hex: %elf
$(BIN) -O ihex $< $@

avr-size $(TRG).elf

#make instruction to delete created files
clean:
$(RM) $(OBJ)
$(RM) $(TRG).map
$(RM) $(TRG).elf
$(RM) $(TRG).obj
$(RM) $(TRG).hex
$(RM) *.bak
$(RM) *.lst
$(RM) ../*.lst
$(RM) *.?_sym

clean_before:
$(RM) $(TRG).hex
$(RM) *.err

 


จ๊าบไหมผิดถูกตรงไหนก็บอกกันได้ ผมแค่งูๆ ปลาๆทีนี้ไม่ต้องกลัว จะปิดตรงไหน เปิดตรงไหน แล้วแต่

แต่เปิดดีกว่านะจะได้นำไปพัฒนาต่อได้อีกจริงไหม