微知-C语言如何指定弱符号?(#define WEAK __attribute__((weak)); WEAK int foo(void))

背景

在C语言中,弱符号是一种特殊的符号,它可以被重定义,但在链接时,只会使用被重定义的符号。
在某些情况下,我们需要在多个文件中定义相同的函数,但只使用其中一个函数。本文将介绍如何在C语言中实现弱符号。

方法

在C语言中,我们可以通过定义宏来指定弱符号,如下:

//file1.c 一来foo函数
#define WEAK __attribute__((weak))
WEAK int foo(void)
{
    printf("file1.c foo\n");
    return 0;
}

int bar(void)
{
    return foo();
}

//file2.c 提供真正的foo函数
int foo(void)
{
    printf("file2.c foo\n");
    return 1;
}

// main.c 将file1.c和file2.c链接在一起
#include "stdio.h"
int main(void)
{
    return bar();
}

Makefile:

//Makefile 编译链接
# 指定编译器
CC=gcc

# 指定编译选项
CFLAGS=-Wall -Wextra -pedantic -std=c99

# 默认目标
.DEFAULT_GOAL := all

# 目标文件
TARGET = main

# 源文件
SOURCES = main.c file1.c file2.c

# 目标文件
OBJECTS = $(SOURCES:.c=.o)

# 默认目标,构建程序
all: $(TARGET)

# 链接目标文件生成可执行文件
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $@

# 编译每个源文件生成目标文件
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# 清理生成的文件
clean:
	rm -f $(OBJECTS) $(TARGET)

# 防止make自动生成*.o文件
.PHONY: all clean

实操

在这里插入图片描述

Build started: Project: gpio_toggle_output_LP_MSPM0G3507_nortos_keil *** Using Compiler 'V6.5', folder: 'D:\keil32\ARM\ARMCLANG\Bin' Build target 'gpio_toggle_output_LP_MSPM0G3507_nortos_keil' Before Build - User command #1: cmd.exe /C "C:\Users\liang\Desktop\电赛\mspm0\examples\ganwei_wu_mcu\MSPM0G3507_Demo\No_mcu\gpio_toggle_output\keil\../../../../../../tools/keil/syscfg.bat 'C:\Users\liang\Desktop\电赛\mspm0\examples\ganwei_wu_mcu\MSPM0G3507_Demo\No_mcu\gpio_toggle_output\keil\' gpio_toggle_output.syscfg" 'H' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 文件名、目录名或卷标语法不正确。 'SysConfig宸ュ叿锛岃妫€鏌ヨ矾寰勬槸鍚︽纭紒' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '瀹為檯璺緞锛?SYSCFG_CLI_PATH' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 ArmClang.exe: error: unsupported argument 'armasm' to option 'Wa,' ArmClang.exe: error: unsupported argument '--pd' to option 'Wa,' ArmClang.exe: error: unsupported argument '__UVISION_VERSION SETA 538' to option 'Wa,' ArmClang.exe: error: unsupported argument 'armasm' to option 'Wa,' ArmClang.exe: error: unsupported argument '--pd' to option 'Wa,' ArmClang.exe: error: unsupported argument '__MSPM0G3507__ SETA 1' to option 'Wa,' assembling startup_mspm0g350x_uvision.s... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] 1 warning generated. compiling ADC.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] 1 warning generated. compiling Uart.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] 1 warning generated. compiling gpio_toggle_output.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] 1 warning generated. compiling No_Mcu_Ganv_Grayscale_Sensor.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] 1 warning generated. compiling Time.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] ../ti_msp_dl_config.c(47): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_init(void) ^ ../ti_msp_dl_config.c(47): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_init(void) ^ ../ti_msp_dl_config.c(62): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_initPower(void) ^ ../ti_msp_dl_config.c(62): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_initPower(void) ^ ../ti_msp_dl_config.c(82): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_GPIO_init(void) ^ ../ti_msp_dl_config.c(82): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_GPIO_init(void) ^ ../ti_msp_dl_config.c(139): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_SYSCTL_init(void) ^ ../ti_msp_dl_config.c(139): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_SYSCTL_init(void) ^ ../ti_msp_dl_config.c(184): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_TIMER_1_init(void) { ^ ../ti_msp_dl_config.c(184): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_TIMER_1_init(void) { ^ ../ti_msp_dl_config.c(206): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_I2C_0_init(void) { ^ ../ti_msp_dl_config.c(206): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_I2C_0_init(void) { ^ ../ti_msp_dl_config.c(244): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_UART_0_init(void) ^ ../ti_msp_dl_config.c(244): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_UART_0_init(void) ^ ../ti_msp_dl_config.c(268): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_ADC1_init(void) ^ ../ti_msp_dl_config.c(268): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_ADC1_init(void) ^ ../ti_msp_dl_config.c(282): error: unknown type name 'SYSCONFIG_WEAK' SYSCONFIG_WEAK void SYSCFG_DL_SYSTICK_init(void) ^ ../ti_msp_dl_config.c(282): error: expected identifier or '(' SYSCONFIG_WEAK void SYSCFG_DL_SYSTICK_init(void) ^ 1 warning and 18 errors generated. compiling ti_msp_dl_config.c... ".\Objects\gpio_toggle_output_LP_MSPM0G3507_nortos_keil.axf" - 24 Error(s), 6 Warning(s). Target not created. Build Time Elapsed: 00:01:29
07-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值