Warum geht GCC Extern hier nicht?

Gast #4678810
Lesenswert?
• ▲
▼
1
//main.c
2
static const struct _module_config
3
{
4
    uint16_t     passkey[8];
5
    uint8_t      mac[6];        // 6b  Mac Address of Module
6
} module_config = { {0} , {0xAA,0xBB,0xCC,0x11,0x22,0x33}   };
7

8

9
//xx.c
10
extern _module_config module_config;
11

12
put(&module_config.mac[0],0);


error: 'module_config' undeclared (first use in this function)

Wieso geht extern hier nicht?
Gast #4678815
Lesenswert?
• ▲
▼
Du musst die Typ-Definition deines Structs in ein Header-File auslagern.
in das kann dann auch die "extern"-Zeile.

in main.c dann die Variable anlegen und initialisieren, aus xx.c kannst 
du dann darauf zugreifen.

(in xx.c muss auch bekannt sein, wie die struct intern aussieht, also 
z.B. dass es darin ein Member "mac" gibt)
#4678820
Lesenswert?
• ▲
▼
Simon schrieb:
> //main.c
> static const struct _module_config

static heißt, dass der Namne von außen nicht sichtbar ist. Warum hast du 
das da hingeschrieben?

> {
>     uint16_t     passkey[8];
>     uint8_t      mac[6];        // 6b  Mac Address of Module
> } module_config = { {0} , {0xAA,0xBB,0xCC,0x11,0x22,0x33}   };
>
> //xx.c
> extern _module_config module_config;

Hier versuchst du, von außen den Namen zu benutzen, den du oben explizit 
gegen die Nutzung von außen gesperrt hast.
Das ist aber nicht der Grund für die Fehlermeldung. Wie nicht"Gast" oben 
schon geschrieben hat, heißt der Typ nicht _module_config, sondern 
struct _module_config, und der Typ muss natürlich in xx.c auch bekannt 
sein.
Gast #4678834
Lesenswert?
• ▲
▼
1
config.h
2

3
extern const struct _module_config
4
{
5
    uint16_t     passkey[8];
6
    uint8_t      mac[6];        // 6b  Mac Address of Module
7
} module_config;
8

9
alle_anderen.c
10

11
const struct _module_config module_config = { {0} , {0xAA,0xBB,0xCC,0x11,0x22,0x33}   };


Scheint so zu gehen
#4678944
Lesenswert?
• ▲
▼
Zwei kleine Hinweise...

Simon schrieb:
>...

Wenn man das "ausführlich" schreibt, ist das also:
1
struct _module_config
2
{
3
  uint16_t passkey[8];
4
   uint8_t     mac[6];
5
} ;
6

7
extern const struct _module_config module_config ;

Bezeichner "sollten" in C allerdings nicht mit einem Unterstrich 
beginnen. (Um potentielle Konflikte mit vom Compiler definierten Namen 
zu vermeiden.)

nicht"Gast" schrieb:
> und wieder...
>
> das static muss natürlich auch weg, wenn du das struct in anderen
> Dateien benutzen willst.

Manchmal kann es sinnvoll sein (static const) Variablen im Header zu 
instantiieren. Man erhält in jedem Object File eine (identische) 
Instanz. Durch das "static" gibt es keine Namenskonflikte. Man könnte 
also durchaus in den Header schreiben:
1
static const struct _module_config = {{0},{0xAA,0xBB,0xCC,0x11,0x22,0x33}};

Edit: Wie im Eingangsposting gezeigt. Auf das "extern" verzichtet man 
dann. Wenn man das extern richtig deklariert stört es aber auch nicht.
Gast #4679082
Lesenswert?
• ▲
▼
Bau dir doch eine Schnittstelle, dann brauchst du auch kein extern. Das 
könnte dann so, oder so ähnlich aussehen:
1
/*  ==================================================  */
2
/*  Modul A Header: mod_a.h                             */
3

4
#include <stdint.h>
5

6
#define PASSKEY_LEN     16
7
#define MAC_LEN         6
8

9

10
struct mod_conf_t
11
{
12
    uint16_t passkey[ PASSKEY_LEN ];
13
    uint8_t  mac[ MAC_LEN ];
14
};
15

16

17
void get_passkey(uint16_t *buff);
18
void get_mac(uint8_t* buff);
19

20
void set_passkey(uint16_t value, uint8_t index);
21
void set_mac(uint8_t value, uint8_t index);
22

23

24
/*  ==================================================  */
25
/*  Modul A: mod_a.c                                    */
26

27
#include <string.h>
28

29
#include "mod_a.h"
30

31

32
static struct mod_conf_t mein_globales_struct = {
33
                                {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
34
                                {0,1,2,3,4,5}
35

36
};
37

38

39
void get_passkey(uint16_t *buff)
40
{
41
    memcpy(buff, mein_globales_struct.passkey, PASSKEY_LEN * 2);
42
}
43

44

45
void get_mac(uint8_t* buff)
46
{
47
    memcpy(buff, mein_globales_struct.mac, MAC_LEN);
48
}
49

50

51
void set_passkey(uint16_t value, uint8_t index)
52
{
53
    mein_globales_struct.passkey[index] = value;
54
}
55

56

57
void set_mac(uint8_t value, uint8_t index)
58
{
59
    mein_globales_struct.mac[index] = value;
60
}
61

62

63
/*  ==================================================  */
64
/*  Modul B: main.c                                     */
65

66
#include <stdio.h>
67
#include <stdint.h>
68

69
#include "mod_a.h"
70

71
int main(void)
72
{
73
    uint16_t key[ PASSKEY_LEN ];
74
    uint8_t  mac[ MAC_LEN ];
75

76
    get_passkey(key);
77
    get_mac(mac);
78

79
    printf("Key is:");
80
    for(uint8_t i = 0; i < PASSKEY_LEN; i++) {
81
        printf(" 0x%X", key[ i ]);
82
    }
83
    printf("\n");
84

85
    printf("Mac is:");
86
    for(uint8_t i = 0; i < MAC_LEN; i++) {
87
        printf(" 0x%X", mac[ i ]);
88
    }
89
    printf("\n");
90

91

92
    for(uint8_t i = 0; i < PASSKEY_LEN; i++) {
93
        set_passkey(0xBEEF + i, i);
94
    }
95

96
    for(uint8_t i = 0; i < MAC_LEN; i++) {
97
        set_mac(0x10 + i, i);
98
    }
99

100

101
    get_passkey(key);
102
    get_mac(mac);
103

104
    printf("Key is:");
105
    for(uint8_t i = 0; i < PASSKEY_LEN; i++) {
106
        printf(" 0x%X", key[ i ]);
107
    }
108
    printf("\n");
109

110
    printf("Mac is:");
111
    for(uint8_t i = 0; i < MAC_LEN; i++) {
112
        printf(" 0x%X", mac[ i ]);
113
    }
114
    printf("\n");
115

116
    return 0;
117
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren