具有多个定义的嵌入式C和AVR GCC编译问题

戴维·兹比

看来我的编译器不愿意在其中构建固件,stepper_motor.c并且正如您在下面看到的那样,它引发了此错误,我无法解决。

我创建了一个stepper_motor.c放置所有函数和变量源,以及一个放置stepper_motor.h所有#defines和函数原型标头在其中main.c,仅包括stepper_motor.h标题并使用功能。从shell日志数据可以看到,它不会编译,并告诉您多次定义了4个变量。那些用于ISR()例程中的计时器,因此它们也需要易变。

任何信息将不胜感激在这个问题上。

这是我的main.c包括:--------

#include <avr/io.h>
#include <Hardware_Bay.h>
#include <util/delay.h>
#include <USART.h>
#include <string.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/interrupt.h>
#include <scale16.h>
#include <stepper_motor.h>

const uint8_t motor_Phases[] = {
    (1 << COIL_B1),                             // full step
    (1 << COIL_B1) | (1 << COIL_A2),            // half step
    (1 << COIL_A2),                             // full step
    (1 << COIL_A2) | (1 << COIL_B2),            // half step
    (1 << COIL_B2),                             // full step
    (1 << COIL_B2) | (1 << COIL_A1),            // etc..
    (1 << COIL_A1),
    (1 << COIL_A1) | (1 << COIL_B1),
};

volatile uint8_t stepPhase = 0;
volatile uint16_t stepCounter = 0;
volatile int8_t direction = FORWARD;

ISR(TIMER0_COMPA_vect) {                    // Timer/Counter-0 Compare match interrupt vector enable
    stepPhase += direction;                 // take step in right direction
    stepPhase &= 0b00000111;                // keep the stepPhase in range (0 - 7)
    STEPPER_PORT = motor_Phases[stepPhase]; // write phase out to motor COIL-1 A/B
    HALL_PORT = motor_Phases[stepPhase];    // write phase out to motor COIL-2 A/B
    stepCounter ++;
}

------------------------------头文件------------------ -----------------------------

#ifndef STEPPER_MOTOR_H_INCLUDED
#define STEPPER_MOTOR_H_INCLUDED

#endif // STEPPER_MOTOR_H_INCLUDED

#define FORWARD           1                      
#define BACKWARD         -1
#define TURN              200  

#define MAX_DELAY         255                                                  
#define MIN_DELAY         10                                                      
#define ACCELERATION      16                                        

#define RAMP_STEPS        (MAX_DELAY - MIN_DELAY) / ACCELERATION

void stepperDrive(uint8_t number_of_steps, uint8_t delay);

void trapezoidDrive_Stepper(int16_t number_of_steps);
PS D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6> make flash
avr-gcc -Wl,-Map,Physalis_GEO_version_3.6.map -Wl,--gc-sections -mmcu=atmega1284p fuse.o main.o stepper_motor.o USART.o  -o Physalis_GEO_version_3.6.elf
stepper_motor.o: In function `stepperDrive':
D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6/stepper_motor.c:50: multiple definition of `stepCounter'
main.o:(.bss.stepCounter+0x0): first defined here
stepper_motor.o:(.data.direction+0x0): multiple definition of `direction'
main.o:(.data.direction+0x0): first defined here
stepper_motor.o:(.rodata.motor_Phases+0x0): multiple definition of `motor_Phases'
main.o:(.rodata.motor_Phases+0x0): first defined here
stepper_motor.o: In function `stepperDrive':
D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6/stepper_motor.c:50: multiple definition of `stepPhase'
main.o:(.bss.stepPhase+0x0): first defined here
make: *** [Physalis_GEO_version_3.6.elf] Error 1
PS D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6>
克利福德

确实,错误消息告诉您所有您需要了解的内容-您已经在两个位置定义了这些符号-main.c和setpper_motor.c。

您已经stepCounter在stepper_motor.c的第50行中定义了,并且也如main.c中所示。stepPase和和类似motor_Phases

  • 如果它们是独立的并且在外部不可见,则应同时声明两者,static以便它们仅在各自的翻译单元中可见。

  • 如果要使符号引用它们是相同的对象,则需要声明其中之一extern以向编译器指示其类型和名称,但必须在其他位置定义

  • 如果它们是独立的,但“需要”是全局的(因为您无法想到更好的解决方案),则它们不能具有相同的名称。

  • 如果您未在main.c中引用它们,则无论如何应从那里删除它们。当它们与步进电机控制明确相关时,在这里定义它们似乎很奇怪,应该将它们封装在stepper_motor.c中。

避免在第一手使用globals是一个更好的解决方案,请参见https://www.embedded.com/a-pox-on-globals/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章