gpt4 book ai didi

c - gcc 使用 -Os 生成大小为 0 的二进制文件

转载 作者:太空狗 更新时间:2023-10-29 15:32:38 25 4
gpt4 key购买 nike

我有一个简单的 FreeRTOS 应用程序,它只在主循环中切换 LED。当我用 -Os 编译它时,生成的二进制文件大小为 0。没有 -Os 一切都按预期工作。这里发生了什么?

我的 CFLAGS 是:

CPUFLAG = -mthumb -mcpu=cortex-m4
FPUFLAG = -mfpu=fpv4-sp-d16 -mfloat-abi=hard
WFLAG = -Wall -Wextra -Werror -Wstrict-prototypes

CFLAGS += -std=gnu99 $(WFLAG) $(CPUFLAG) $(FPUFLAG) -mlittle-endian -mthumb -nostartfiles
CFLAGS += -ffunction-sections -fdata-sections -fno-builtin
LDFLAGS += -nostdlib --gc-sections -static

main 基本上是 TI 的闪烁演示:

int main(void)
{
volatile uint32_t ui32Loop;

//
// Enable the GPIO port that is used for the on-board LED.
//
SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;

//
// Do a dummy read to insert a few cycles after enabling the peripheral.
//
ui32Loop = SYSCTL_RCGC2_R;

//
// Enable the GPIO pin for the LED (PF3). Set the direction as output, and
// enable the GPIO pin for digital function.
//
GPIO_PORTF_DIR_R = 0x08;
GPIO_PORTF_DEN_R = 0x08;

//
// Loop forever.
//
while(1)
{
//
// Turn on the LED.
//
GPIO_PORTF_DATA_R |= 0x08;

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}

//
// Turn off the LED.
//
GPIO_PORTF_DATA_R &= ~(0x08);

//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
{
}
}

return 0;
}

使用 -Os 这会生成

   text    data     bss     dec     hex filename
0 0 0 0 0 image.elf

否则

   text    data     bss     dec     hex filename
2012 4 728 2744 ab8 image.elf

编辑:differences in .map files

最佳答案

由于您指定了 -nostartfiles,因此未使用标准启动及其入口点,因此没有对您的 main 函数的引用,并且 --gc -sections 将整个部分丢弃为未使用。

要修复,请尝试将函数命名为 start 或将 -e _main 添加到 ld 标志。

关于c - gcc 使用 -Os 生成大小为 0 的二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26382516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com