简体   繁体   中英

Compile error with instantiation of typedef struct type (C language)

I'm mystified by a problem I'm encountering. I'm controlling a bunch of relays with a Raspberry Pi, to facilitate ease of turning the relays on and off I've created a struct definition for the relay gpio pins. Here is the complete content of the header file:

#ifndef PIN_DEFINITIONS
#define PIN_DEFINITIONS

#define LTC2057_SHDN            2   // physical pin 3
#define K1_SET_SIGNAL           4   // physical pin 7
#define K7_SET_SIGNAL           5   // physical pin 29
#define RES_ARRAY_RESET_SIGNAL  6   // physical pin 31
#define K15_SET_SIGNAL          7   // physical pin 26
#define BANK_A_RESET_SIGNAL     8   // physical pin 24
#define K5_SET_SIGNAL           9   // physical pin 21
#define K4_SET_SIGNAL           10  // physical pin 19
#define K6_SET_SIGNAL           11  // physical pin 23
#define K14_SET_SIGNAL          12  // physical pin 32
#define K8_SET_SIGNAL           13  // physical pin 33
#define UART_TX                 14  // physical pin 8
#define UART_RX                 15  // physical pin 10
#define K13_SET_SIGNAL          16  // physical pin 36
#define K1_RESET_SIGNAL         17  // physical pin 11
#define CAL_RESET_SIGNAL        18  // physical pin 12
#define K9_SET_SIGNAL           19  // physical pin 35
#define K12_SET_SIGNAL          20  // physical pin 38
#define K11_SET_SIGNAL          21  // physical pin 40
#define K3_SET_SIGNAL           22  // physical pin 15
#define K17_SET_SIGNAL          23  // physical pin 16
#define K16_SET_SIGNAL          24  // physical pin 18
#define BANK_B_RESET_SIGNAL     25  // physical pin 22
#define K10_SET_SIGNAL          26  // physical pin 37
#define K2_SET_SIGNAL           27  // physical pin 13


typedef struct RELAY_STRUCTURE
{
    int bcm_set_pin;
    int bcm_reset_pin;
    int relay_number;
    int physical_set_pin;
    int physical_reset_pin;
} RELAY;

RELAY K1;
RELAY K2;
RELAY K3;
RELAY K4;
RELAY K5;
RELAY K6;
RELAY K7;
RELAY K8;
RELAY K9;
RELAY K10;
RELAY K11;
RELAY K12;
RELAY K13;
RELAY K14;
RELAY K15;

K1.bcm_set_pin = K1_SET_SIGNAL;
K1.bcm_reset_pin = K1_RESET_SIGNAL;
K1.relay_number = 1;
K1.physical_set_pin = 7;
K1.physical_reset_pin = 11;

K2.bcm_set_pin = K2_SET_SIGNAL;
K2.bcm_reset_pin = RES_ARRAY_RESET_SIGNAL;
K2.relay_number = 2;
K2.physical_set_pin = 13;
K2.physical_set_pin = 31;

K3.bcm_set_pin = K3_SET_SIGNAL;
K3.bcm_reset_pin = RES_ARRAY_RESET_SIGNAL;
K3.relay_number = 3;
K3.physical_set_pin = 15;
K3.physical_set_pin = 31;

K4.bcm_set_pin = K4_SET_SIGNAL;
K4.bcm_reset_pin = RES_ARRAY_RESET_SIGNAL;
K4.relay_number = 4;
K4.physical_set_pin = 19;
K4.physical_set_pin = 31;

K5.bcm_set_pin = K5_SET_SIGNAL;
K5.bcm_reset_pin = RES_ARRAY_RESET_SIGNAL;
K5.relay_number = 5;
K5.physical_set_pin = 9;
K5.physical_set_pin = 31;

#endif

The problem is with this header file, "Pin_Defs.h". If I try to compile this file individually I get a compile error when trying to assign values to instantiation of the relay struct:

Pin_Defs.h:92:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 K1.bcm_set_pin = K1_SET_SIGNAL;
   ^
Pin_Defs.h:93:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 K1.bcm_reset_pin = K1_RESET_SIGNAL;
   ^
Pin_Defs.h:94:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 K1.relay_number = 1;
   ^
Pin_Defs.h:95:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 K1.physical_set_pin = 7;
   ^
Pin_Defs.h:96:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 K1.physical_reset_pin = 11;

If the pin assignments are moved to a different file, say main.c, and then #include Pin_Defs.h from main.c, everything compiles just fine. I'm baffled as to what the problem is. There's no missing braces or semicolon that I'm aware of, as the only difference is putting the instantiation assignment in a different file. Anybody have any insights? I have it working by putting in a separate file, I'm just trying to understand the root of the issue better. Thanks very much

You're performing executable statements, in this case expressions performing an assignment, outside of a function. Only declarations and definitions with an optional initializer are allowed outside a function.

What you want to do is initialize the variables like this:

RELAY K1 = { K1_SET_SIGNAL, K1_RESET_SIGNAL, 1, 7, 11 };

Also, it's not a good idea to put definitions inside of a header file. If multiple source files include this header you'll end up with several multiple definition errors. So you'll want to put extern declarations in the header and the definitions in exactly one source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM