C Question - SDCC Compiler

Status
Not open for further replies.

Jon Wilder

Active Member
I've always been taught that the typical practice for external functions shared across modules is to define in source files and declare in header files. However, I have a struct that I'm trying to share across modules, and the only way I've been able to make the compiler not complain is to define the struct in the header file, then declare the struct in the source files.

Code:
/* midi.h */

struct MIDI {
    uint8_t buffer, controller, value;
    bool controller_received:1, status_valid:1; program_change:1, control_change:1, data_ready:1, store:1, mute:1, channel_select:1;
};

Code:
/* midi.c */
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

extern struct MIDI midi;

void receiveSerial() __interrupt(SI0_VECTOR) __using 1 {
    /* Rest of code */
}

Code:
/* main.c*/
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

extern struct MIDI midi;

void main() {
    /* Main Function */
}

When I instead try to do -

Code:
/* midi.c */
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

struct MIDI {
    uint8_t buffer, controller, value;
    bool controller_received:1, status_valid:1; program_change:1, control_change:1, data_ready:1, store:1, mute:1, channel_select:1;
};

void receiveSerial __interrupt(SI0_VECTOR) __using 1 {
    struct MIDI midi;
    /* Rest of code */
}

Code:
/* midi.h */

struct MIDI;

Code:
/* main.c */
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

extern struct MIDI midi;

void main() {
    /* Main Function */
}

This does not work.

Can anyone explain why?
 
Last edited:
Have you inclulded midi.h?

Mike.

Yes I have. Just edited my post to include my #include heirarchy.

As I stated above, if I define the struct in midi.h, then declare the struct in midi.c and main.c, it works. But I'd rather define the struct in midi.c, then declare it in midi.h and as extern in main.c.
 
With some reading and research, I figured it out. Because structs are like classes in C++, the entire struct body must be declared in the header file in order to access the struct members. You then declare the instance variable once as an extern in the header file, and once as a global in a source file. The linker will do the magic from there.

I've just tested this and it works flawless.

Code:
/* midi.h */

struct Foo {
    bool bar;
};

extern struct Foo foo;

Code:
/* midi.c */
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

struct Foo foo;

void receiveSerial __interrupt(SI0_VECTOR) __using 1 {
    /* Interrupt Code */
}

Code:
/* main.c */
#include <at89s8253.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "midi.h"

void main() {
    /* Main Function */
    foo.bar = false;
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…