我尝试使用函数的输出初始化全局变量。它与gnu-gcc一样正常工作,但是当使用avr-gcc编译时,变量初始化为0.我希望变量由函数初始化,因为在实际场景中它是一个引用(MyClass& myclass = MyClass::getInstance()
。
以下是代码:
extern "C"
{
#include "led.h"
}
int getInteger()
{
return 10;
}
int my_int = getInteger();
int main (void)
{
LED_On(LED2);
//my_int = getInteger(); //* This line really sets my_int = 10;
if(my_int == 10)
{
LED_On(LED1);
}
while(1){
__asm__ ("nop");
}
return 0;
}
这是我的Makefile:
PROJ_NAME=TEST
# Include paths for project folder
SRCS_INC += -I. -I./preprocessor
# Sources files for project folder (*.c and *.cpp)
SRCS += main.cpp exception_noNMI.S startup_uc3.S trampoline_uc3.S intc.c led.c
AS = avr32-gcc
ASFLAGS = -x assembler-with-cpp -c -mpart=uc3c1512c -mrelax
ASFLAGS += ${SRCS_INC}
CC = avr32-gcc
CFLAGS += -mpart=uc3c1512c
CFLAGS += ${SRCS_INC}
CXX = avr32-g++
LINKER = avr32-g++
OBJCOPY = avr32-objcopy
LDFLAGS = -nostartfiles -mpart=uc3c1512c
LDFLAGS += ${SRCS_INC}
OBJS += $(addsuffix .o, $(basename $(SRCS)))
# Main rule
all: $(PROJ_NAME).elf
# Linking
${PROJ_NAME}.elf: ${OBJS}
@echo Linking...
@$(CXX) $(LDFLAGS) $^ -o $@
@$(OBJCOPY) -O ihex ${OBJCOPYFLAGS} $(PROJ_NAME).elf $(PROJ_NAME).hex
@$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
# Assembly files in source folder
%.o: ./%.S
@mkdir -p $(dir $@)
@$(AS) $(ASFLAGS) -c $< -o $@
# C files in source folder
%.o: ./%.c
@mkdir -p $(dir $@)
@$(CC) -c $< -o $@ $(CFLAGS)
# CPP files in SRC folder
%.o: ./%.cpp
@mkdir -p $(dir $@)
@$(CXX) -c $< -o $@ $(CFLAGS)
.PHONY: clean
clean:
@rm -f $(OBJS) $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin
我还尝试使用__attribute(constructor
创建一个init函数,如this answer中所述。
仍然不是原谅。
答案 0 :(得分:1)
据我所知,您没有启用优化。因此,编译器实际上可能在启动期间调用getInteger。现在,您将使用未显示的内容替换默认启动。也许您只需要确保在启动期间完成此操作?此外,您可以尝试启用优化并查看呼叫是否已解决。