我正在使用Launchpad Arm编译器工具。具体来说,
来自: arm-none-eabi-g ++和arm-none-eabi-gcc:(适用于ARM嵌入式处理器的GNU工具)5.2.1 20151202(发布)[ARM / embedded-5-branch revision 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引用。构建是用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,但是它们太复杂了我无法解开。
答案 0 :(得分:2)
C ++要求数学函数是基本运行时的一部分,而C允许它们在库中。 GCC实现通过在C ++构建中自动链接libm
来实现此目的。
链接阶段还有许多其他差异;如果使用C链接器,C ++链接将始终失败。
对于C链接,请使用C链接器并指定-lm
以使libm
可用。