gpt4 book ai didi

c++ - 链接成功与 arm-none-eabi-g++ 但不是 arm-none-eabi-gcc

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

我正在使用 Launchpad Arm 编译器工具。具体来说,

arm-none-eabi-g++ 和 arm-none-eabi-gcc 来自:

(ARM 嵌入式处理器的 GNU 工具)5.2.1 20151202(发布)[ARM/embedded-5-branch 修订版 231848]

我有一个针对 STM32F103 处理器的简单程序,除了证明我可以编写硬件并调用数学库中的函数外,没有其他用途。这就是全部:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "stm32f10x.h"

void hardwareTest(void){
// Turn on the clock for PortB
RCC->APB2ENR = RCC_APB2ENR_IOPBEN; // Turn on IO Port B
// Put PB0 into push pull 50 MHz mode
GPIOB->CRL = 0x03;
// Turn PB0 on
GPIOB->ODR = 1;
}

volatile int x; // force call to sqrt() later

int main(void) {
x = sqrt(100.0f);
x = sqrt(x);
hardwareTest();
return (x);
}

当我尝试构建它时,出现链接器错误,告诉我存在对 sqrt 的 undefined reference 。构建是使用 arm-none-eabi-gcc 完成的。我偶然发现,如果构建是使用 arm-none-eabi-g++ 完成的,使用相同的命令行参数,则链接会成功执行。

我写了一个 Makefile 来演示区别:

PROJECT = minimal
SOURCES = src/startup_stm32f10x_hd.s \
src/system_stm32f10x.c \
src/main.c
OUTPUT = ./out
print-%:
@echo '$*=$($*)'

TOOLCHAIN = arm-none-eabi-

CXX = $(TOOLCHAIN)g++
CC = $(TOOLCHAIN)gcc
AR = $(TOOLCHAIN)ar
AS = $(TOOLCHAIN)gcc -c -x assembler-with-cpp
LD = $(TOOLCHAIN)gcc
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
RM = rm -f

CFLAGS = -O
CFLAGS += -nostartfiles

CXXFLAGS = -O
CXXFLAGS += -nostartfiles

ARCH = -mcpu=cortex-m3 -mthumb -DSTM32F10X_HD
LDFLAGS =

all: clean $(PROJECT).elf $(PROJECT).gcc $(PROJECT).bin

$(PROJECT).bin: $(PROJECT).elf
@echo ' ======== '
@echo ' Generating binaries'
$(OBJCOPY) -O binary $(OUTPUT)/$< $(OUTPUT)/$(PROJECT).bin
$(OBJCOPY) -O ihex $(OUTPUT)/$< $(OUTPUT)/$(PROJECT).hex
@echo ' ======== '

$(PROJECT).elf: $(SOURCES)
@echo ' ======== '
@echo ' Successful build uses g++'
@echo ' CXXFLAGS = $(CXXFLAGS)'
@echo ' LDFLAGS = $(LDFLAGS)'
@echo ' ARCH = $(ARCH)'
$(CXX) -o $(OUTPUT)/$@ $(ARCH) $(CXXFLAGS) $(LDFLAGS) -Wl,-Tld_script/stm32.ld,-lm $^
@echo ' ======== '

$(PROJECT).gcc: $(SOURCES)
@echo ' ======== '
@echo ' Broken build uses gcc'
@echo ' CFLAGS = $(CFLAGS)'
@echo ' LDFLAGS = $(LDFLAGS)'
@echo ' ARCH = $(ARCH)'
$(CC) -o $(OUTPUT)/$@ $(ARCH) $(CFLAGS) $(LDFLAGS) -Wl,-Tld_script/stm32.ld,-lm $^
@echo ' ======== '

$(PROJECT).gxx: $(SOURCES)
@echo ' ======== '
@echo ' build with g++'
$(CXX) -o $(OUTPUT)/$@ $(ARCH) $(CXXFLAGS) $(LDFLAGS) -Wl,-Tld_script/stm32.ld $^
@echo ' ======== '

# Program the binary to the board using the builtin serial bootloader
program:
stm32loader.py -p /dev/ttyUSB0 -ewv $(OUTPUT)/$(PROJECT).bin

# Remove the temporary files
clean:
@echo ' '
@echo ' Cleaning up: '
$(RM) $(OUTPUT)/* *.o *.elf *.bin *.hex *.gcc *.gxx *.g++
@echo ' ======== '

结果如下:

Cleaning up: 
rm -f ./out/* *.o *.elf *.bin *.hex *.gcc *.gxx *.g++
========
========
Successful build uses g++
CXXFLAGS = -O -nostartfiles
LDFLAGS =
ARCH = -mcpu=cortex-m3 -mthumb -DSTM32F10X_HD
arm-none-eabi-g++ -o ./out/minimal.elf -mcpu=cortex-m3 -mthumb -DSTM32F10X_HD -O -nostartfiles -Wl,-Tld_script/stm32.ld,-lm src/startup_stm32f10x_hd.s src/system_stm32f10x.c src/main.c
========
========
Broken build uses gcc
CFLAGS = -O -nostartfiles
LDFLAGS =
ARCH = -mcpu=cortex-m3 -mthumb -DSTM32F10X_HD
arm-none-eabi-gcc -o ./out/minimal.gcc -mcpu=cortex-m3 -mthumb -DSTM32F10X_HD -O -nostartfiles -Wl,-Tld_script/stm32.ld,-lm src/startup_stm32f10x_hd.s src/system_stm32f10x.c src/main.c
/var/folders/t4/dv7b46055cjgknp4nndn1_zr0000gn/T//ccbl4swG.o: In function `main':
main.c:(.text+0x28): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
make: *** [minimal.gcc] Error 1
========
Generating binaries
arm-none-eabi-objcopy -O binary ./out/minimal.elf ./out/minimal.bin
arm-none-eabi-objcopy -O ihex ./out/minimal.elf ./out/minimal.hex
make: Target `all' not remade because of errors.

那么谁能告诉我为什么这两个编译器的行为不同?我忽略了什么简单的事情?如果我想使用 arm-none-eabi-gcc,我应该如何确保与 libm 和其他人的正确链接?

我看过 Freddie Chopin 的 makefile,但它们太复杂了,我无法理解。

最佳答案

C++ 要求数学函数是基本运行时的一部分,而 C 允许它们位于库中。 GCC 实现通过在 C++ 构建中自动链接 libm 实现了这一点。

链接阶段还有很多其他的区别;如果使用 C 链接器,C++ 链接将始终失败。

对于 C 链接,使用 C 链接器并指定 -lm 以使 libm 可用。

关于c++ - 链接成功与 arm-none-eabi-g++ 但不是 arm-none-eabi-gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993968/

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