chipKIT® Development Platform

Inspired by Arduino™

Variables in Board_Defs.h

Created Fri, 01 Nov 2013 23:43:28 +0000 by mikes


mikes

Fri, 01 Nov 2013 23:43:28 +0000

I tried to add an array to Board_Defs.h, for a board I am working with, but it caused compile errors.

core.a(wiring.c.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here core.a(wiring_digital.c.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here core.a(main.cpp.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here core.a(pins_arduino.c.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here core.a(task_manager.c.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here core.a(wiring_analog.c.o):(.sdata.arrayofpins+0x0): multiple definition of arrayofpins' aaaa.cpp.o:(.sdata.arrayofpins+0x0): first defined here collect2: ld returned 1 exit status

There are groups of pins on this board that I want to be able to access like an array so that I can loop though them without adding a library, I want it available to every sketch for this board by default. Is there a way to accomplish this?


majenko

Fri, 01 Nov 2013 23:53:21 +0000

You should put the array in Board_Data.c and then an extern reference to it in Board_Defs.h.


mikes

Sat, 02 Nov 2013 03:12:25 +0000

That compiles. :D Any good reason you can do that in a library but not the boards file?

Thanks Mike


majenko

Sat, 02 Nov 2013 10:20:24 +0000

The library is included only once. The Board_Defs.h file is included in multiple places. As a result anything defined variable-wise in there gets defined in each file it is included in.

You should never ever define any variables in a .h file, unless they are static. The same with functions - they should only ever be inline and/or static if you put them in a .h file.

.h files are basically to describe things (declare them), not define them. They describe what is in the associated C file, or they describe structures, classes, etc, or macros. They should never define anything like variables, functions, class members, etc.

A good writeup: [url]http://stackoverflow.com/a/3482966/636360[/url]


mikes

Sat, 02 Nov 2013 14:26:00 +0000

The library is included only once. The Board_Defs.h file is included in multiple places. As a result anything defined variable-wise in there gets defined in each file it is included in.

The Board_Defs.h file has directives to prevent multiple definitions it should not matter how many times the file is included anything within the omitted section should only be included once. I'm not arguing best practices I just wanted to know what was different.

#if !defined(BOARD_DEFS_H)
#define BOARD_DEFS_H
/*
omitted
*/
#endif	// BOARD_DEFS_H

majenko

Sat, 02 Nov 2013 14:39:23 +0000

That only protects against multiple inclusions in the same compilation unit (.o file), not the entire resultant .elf file.


mikes

Mon, 04 Nov 2013 17:45:40 +0000

I did not know that the compilations worked that way. I will have to look closer at how MPIDE actually works at some point. Thanks for the Info Mike


majenko

Mon, 04 Nov 2013 17:50:34 +0000

Every .c or .cpp file is compiled separately into a .o file. Some of the .o files are grouped into archives - .a files (MPIDE uses one - core.a). That .a file and the remaining .o files are linked together to form a .elf file.

This is standard compilation practice.