Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4919

General discussion • Re: How to make the GPIOD global

$
0
0
My apologies, I misinterpreted the context, due to the nature of the query. I've been a C coder for about 40 years, can't remember the year exactly, but it was on MS-DOS 3.20 on an 8086 CPU, 5.25" floppies, and a huge 20MB ST506 MFM HDD (although I did briefly play around with a ROM based C compiler on a 6502 prior to that). After the 8086, it was on an 8MHz ARM2 with 4MB RAM and several HDDs totalling around 100MB.

The error "Move.c:76:28: error: ‘lines’ undeclared (first use in this function); did you mean ‘linger’?" seemed to indicate someone unfamiliar with global vs local variable scope, so I pitched my response accordingly. Re-reading it, I've realised I did actually miss a subtlety for global variable scope, which could be relevant here. A static global is restricted to the source file where it is declared, so don't declare them as static if they are to be used across multiple source files (declare it in one file with no static or extern modifier, then reference it in other files with a global extern declaration (e.g. "extern struct …"). The static & extern bit might be obvious and known, and is relatively easy for simple variable types, but it's getting the definition of the struct correctly repeated across multiple files that can trip people up, is a bit more advanced, and may be the real issue here. With structs, it's usually better to put the actual structure definition in a header file, so that there's no inconsistency introduced by separately defining the struct in each source file.

Here's a quick example, create lines_struct.h with:

Code:

struct lines_struct {  int a;  int b;};
Then you declare it in one source file with:

Code:

#include "lines_struct.h"struct lines_struct lines;
And reference it in other source files with:

Code:

#include "lines_struct.h"extern struct lines_struct lines;
In your case, the actual structure definition should be coming from a header file supplied with the library, I think. I.e. you shouldn't need your own header file, just include the appropriate library header with the definition in each source file, declare the global in one source file, and reference the global with extern in other files.

Statistics: Posted by Murph9000 — Sat Sep 28, 2024 7:16 am



Viewing all articles
Browse latest Browse all 4919

Trending Articles