avr-gcc:未使用函数输出初始化的全局变量
avr-gcc: Global variable not initialized with function output
我尝试用一个函数的输出来初始化一个全局变量。它在 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
创建一个初始化函数,如 this answer 中所述。
还是没有执行。
据我所知,您没有启用优化。因此,编译器实际上可能会在启动期间调用 getInteger。现在,您要用不显示的内容替换默认启动。也许您只需要确保在启动期间完成此操作?另外,您可以尝试启用优化,看看调用是否已解决。
我尝试用一个函数的输出来初始化一个全局变量。它在 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
创建一个初始化函数,如 this answer 中所述。
还是没有执行。
据我所知,您没有启用优化。因此,编译器实际上可能会在启动期间调用 getInteger。现在,您要用不显示的内容替换默认启动。也许您只需要确保在启动期间完成此操作?另外,您可以尝试启用优化,看看调用是否已解决。