ercircle 发表于 2024-12-3 16:43:52

分享SDCC\IAR下.lib生成与使用经验

# 分享SDCC\IAR下.lib生成与使用经验

## 1. IAR下生成.lib

!(data/attachment/forum/202412/03/163014s5et48meg54pmmm8.png "image.png")

!(data/attachment/forum/202412/03/163142lhbmh75kblo7h53b.png "image.png")

只需要修改如图选项即可。菜单-》Project-》Make

!(data/attachment/forum/202412/03/163202smhyasswgil5z5bo.png "image.png")

## 2. IAR下使用.lib

和keil操作一致,直接添加进工程即可

!(data/attachment/forum/202412/03/163411muonos3n3a6s066s.png "image.png")

## 3 SDCC生成.lib

调用 `sdar`生成.lib

```
Usage: sdar [-]{dmpqrstx} [--plugin <name>] archive-file file...
       sdar -M [<mri-script]
commands:
d            - delete file(s) from the archive
m      - move file(s) in the archive
p            - print file(s) found in the archive
q         - quick append file(s) to the archive
r- replace existing or insert new file(s) into the archive
s            - act as ranlib
t      - display contents of the archive
x         - extract file(s) from the archive
command specific modifiers:
          - put file(s) after
          - put file(s) before (same as )
          - use zero for timestamps and uids/gids
          - use actual timestamps and uids/gids (default)
          - use instance of name
          - truncate inserted file names
          - use full path names when matching
          - preserve original dates
          - display offsets of files in the archive
          - only replace files that are newer than current archive contents
generic modifiers:
          - do not warn if the library had to be created
          - create an archive index (cf. ranlib)
- specify the dependencies of this library
          - do not build a symbol table
          - deprecated, use --thin instead
          - be verbose
          - display the version number
@<file>      - read options from <file>
--target=BFDNAME - specify the target object format as BFDNAME
--output=DIRNAME - specify the output directory for extraction operations
--record-libdeps=<text> - specify the dependencies of this library
--thin       - make a thin archive
optional:
--plugin <p> - load the specified plugin
emulation options:
No emulation specific options
sdar: supported targets: asxxxx srec symbolsrec verilog tekhex binary ihex plugin
```


Makefile如下:

```makefile
ROOT_DIR := ../
TARGET = add
BUILD_DIR =./build/

#obj2占位符抵消../
BUILD_OBJ_DIR =./build/obj/obj2/
PREFIX =

CC = $(PREFIX)sdcc
AS = $(PREFIX)sdas8051 #sdc51


RM = -rm -rf
MAKE = make

MCU_MODEL = -mmcs51
MODEL= --model-large
CODE_SIZE = --code-size 65536# 64KB 的 Flash 存储器
IRAM_SIZE = --iram-size 2048    # 2KB 的内部 SRAM
XRAM_SIZE = --xram-size 32768   # 32KB 的内部扩展 RAM

AS_DEFS =
C_DEFS =

# includes,头文件必须包含在以下文件中
AS_INCLUDES =
INC_PATHS =\
-I$(ROOT_DIR)App \
-I$(ROOT_DIR)User \
-I$(ROOT_DIR)inc

# libraries

USER_SRC =$(ROOT_DIR)Library

C_SOURCES := $(USER_SRC)/add.c

# 对象文件
C_OBJECTS := $(patsubst %.c, $(BUILD_OBJ_DIR)/%.rel, $(C_SOURCES))

# 最终目标文件
ALL_OBJECTS := $(C_OBJECTS)

# 规则
.PHONY: all clean mk

# 主要目标
all: $(BUILD_DIR)/$(TARGET).lib

# 清理
clean:
        -rm -rf $(BUILD_DIR)/*

# 编译
$(BUILD_OBJ_DIR)/%.rel: %.c
        @echo "Compiling $< into $(dir $@)"
        mkdir -p $(dir $@) | $(CC) -c -o $@ $(MCU_MODEL) $(INC_PATHS) $(MODEL) --out-fmt-ihx --debug $<

# 链接生成
$(BUILD_DIR)/$(TARGET).lib: $(ALL_OBJECTS)
        @echo "Compiling $^ into $(dir $@)"
        mkdir -p $(dir $@) | sdar rv $@ $^


# 删除构建目录
clean-build-dir:
        rm -rf $(BUILD_DIR)
```

## 4 SDCC使用.lib

通过 `LDFLAGS`调用lib,-L包含lib库目录,-l链接指定lib

Makefile语法如下:

```makefile
ROOT_DIR := ../
TARGET = downloadfile
BUILD_DIR =./build/

#obj2占位符抵消../
BUILD_OBJ_DIR =./build/obj/obj2/
PREFIX =

CC = $(PREFIX)sdcc
AS = $(PREFIX)sdas8051 #sdc51


RM = -rm -rf
MAKE = make

MCU_MODEL = -mmcs51
MODEL= --model-large
CODE_SIZE = --code-size 65536# 64KB 的 Flash 存储器
IRAM_SIZE = --iram-size 2048    # 2KB 的内部 SRAM
XRAM_SIZE = --xram-size 32768   # 32KB 的内部扩展 RAM


AS_DEFS =
C_DEFS =

# includes,头文件必须包含在以下文件中
AS_INCLUDES =
INC_PATHS =\
-I$(ROOT_DIR)Driver/inc \
-I$(ROOT_DIR)User \
-I$(ROOT_DIR)Library

# libraries
LIBS = -ladd
LIBDIRS = -L../SDCC-lib/build

LIB_SRC =
USER_SRC =$(ROOT_DIR)User
C_SOURCES := $(wildcard   $(ROOT_DIR)Driver/src/*.c$(ROOT_DIR)Driver/isr/*.c    $(ROOT_DIR)App/src/*.c)

C_SOURCES += $(USER_SRC)/Main.c
ASM_SOURCES =
# 对象文件
C_OBJECTS := $(patsubst %.c, $(BUILD_OBJ_DIR)/%.rel, $(C_SOURCES))
ASM_OBJECTS := $(patsubst %.asm, $(BUILD_OBJ_DIR)/%.rel, $(ASM_SOURCES))

# 最终目标文件
ALL_OBJECTS := $(C_OBJECTS) $(ASM_OBJECTS)


# 链接选项
LDFLAGS =$(LIBDIRS) $(LIBS) # 删除未使用的段

# 规则
.PHONY: all clean mk

# 主要目标
all: $(BUILD_DIR)/$(TARGET).hex

# 清理
clean:
        -rm -rf $(BUILD_DIR)/*
# 编译规则
$(BUILD_OBJ_DIR)/%.rel: %.c
        @echo "Compiling $< into $(dir $@)"
        mkdir -p $(dir $@) | $(CC) -c -o $@ $(MCU_MODEL) $(INC_PATHS) $(MODEL) --out-fmt-ihx --debug $<

$(BUILD_OBJ_DIR)/%.rel: %.asm
        @echo "Compiling $< into $(dir $@)"
        mkdir -p $(dir $@) | $(AS) -o $(INC_PATHS) $@ $<

# 链接规则
$(BUILD_DIR)/$(TARGET).hex: $(ALL_OBJECTS)
        @echo "Compiling $^ into $(dir $@)"
        mkdir -p $(dir $@) | $(CC) -o $@ $(MCU_MODEL) $(INC_PATHS) $(MODEL) $(CODE_SIZE) $(IRAM_SIZE) $(XRAM_SIZE) $(LDFLAGS) --out-fmt-ihx $^


# 删除构建目录
clean-build-dir:
        rm -rf $(BUILD_DIR)
```

## 5. 工程文件

[!(/source/plugin/zhanmishu_markdown/template/editor/images/upload.svg) 附件:Project.zip](forum.php?mod=attachment&aid=68042 "attachment")

21cnsound 发表于 2024-12-3 17:28:01

请问SDCC\IAR生成的lib和keil生成的lib通用吗?

huashanhui20 发表于 2024-12-3 19:29:41

21cnsound 发表于 2024-12-3 17:28
请问SDCC\IAR生成的lib和keil生成的lib通用吗?

你赶紧试一下

ercircle 发表于 2024-12-3 19:36:19

21cnsound 发表于 2024-12-3 17:28
请问SDCC\IAR生成的lib和keil生成的lib通用吗?

{:4_245:}试了没,不支持吧

ercircle 发表于 2024-12-3 19:37:50

gpt的解释:

21cnsound 发表于 2024-12-3 21:15:27

ercircle 发表于 2024-12-3 19:37
gpt的解释:

截图这样讲,我倒是有个疑问:STC发布的lib库文件应该是没有区分keil/SDCC/IAR的(下载的lib文件只有一种),不知道用什么编译器生成的?

ercircle 发表于 2024-12-3 21:50:25

21cnsound 发表于 2024-12-3 21:15
截图这样讲,我倒是有个疑问:STC发布的lib库文件应该是没有区分keil/SDCC/IAR的(下载的lib文件只有一种 ...

只能keil下用吧

VCC 发表于 2024-12-3 22:12:57

本帖最后由 VCC 于 2024-12-3 23:02 编辑

21cnsound 发表于 2024-12-3 21:15
截图这样讲,我倒是有个疑问:STC发布的lib库文件应该是没有区分keil/SDCC/IAR的(下载的lib文件只有一种 ...
这个我之前有个帖子探究过

刚刚我调用最新的lib库,搞错一个东西.和大家分享一下我的错误 - 第2页 - C语言,汇编语言,Proteus MCU软件仿真 国芯技术交流网站 - AI32位8051交流社区

结论:STC发布的lib文件是 OMF51 格式的,只能被BL51和LX51这两款Keil PK51工具链里的链接器识别

至于Windows系统里的静态链接库,虽然也是.lib后缀,但那是完全不同的文件格式了


页: [1]
查看完整版本: 分享SDCC\IAR下.lib生成与使用经验