Arduino #ifndef compiler directive issues

Folks,
I'm really at a loss about the compiler directives and they don't seem to work as I would expect.
I've been doing some debugging, but to no avail...

In the example below I use the ifndef tag "GameTableUtils_h"

/******************************************
* GAMETABLE UTILS
* Library to control a 12x12 Game Table
*******************************************/
#pragma once
#ifndef GameTableUtils_h
#define GameTableUtils_h
#ifdef DUMMYTABLE
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <cstdio>
#include <string.h>
#include <sys/time.h>
uint32_t millis(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

This file is included in several other modules and I do get the linking errors:
"...multiple definition of `gtRed'; sketch/GameTable0.7.ino.cpp.o:sketch/lib/GameTableUtils.h:45: first defined here"
Which is odd since I do use the ifndef compiler directive.

But now it is getting really weird:
If I change the directive from "#ifndef GameTableUtils_h" to "#ifndef GTUTIL_H", and obviously also in the "#define" directive. I get a whole bunch of other errors:

In file included from sketch/lib/Game.h:10:0,
                 from /Users/mmaters/Documents/Arduino/GameTable0.7/GameTable0.7.ino:14:
/Users/mmaters/Documents/Arduino/libraries/GameTable/GameTableUtils.h:33:6: error: multiple definition of 'enum Games'
 enum Games : uint8_t { gmNoGame=0, gmTetris=1, gmClock=2, gmSnake=3, gmLife=4, gmText=5, gmSimple=6, gmBricks=7, gmNextGame=8 } ;
      ^

Does anybody knows what's going on?
Help will be really appreciated.

ps. The codes works as intended using a g++ compiler.

Why use both #pragma once and your #ifndef ?

And you forgot #endif's

since .h files are often included in more than one file, variable "definitions" (allocation) and functions should not be defined in .h files, because this results in multiple definitions. they should be defined in .cpp files and the .h file provide extern "declarations" so that when compiling other .ino/.cpp files, the compiler is "aware" of them and can provide proper linkage.

the #ifdef DUMMYTABLE is the type thing done in .h files to prevent them from being processed multiple times since .h files can be included in other .h files.

it's convention to the following in .h files to avoid redundant processing. FILE_H will only be un-defined the first time the .h is processed and ignored subsequently because FILE_H gets defined the first time it is processed

#ifndef FILE_H
# define FILE_H
#endif

Why use both #pragma once and your #ifndef ?

And you forgot #endif's

I don't know, I thought I'll use both, but that doesn't seem to work either.
#endif's are included, at the end of the file, but it's a rather long piece of code and I've only included the first part.

since .h files are often included in more than one file, variable "definitions" (allocation) and functions should not be defined in .h files, because this results in multiple definitions. they should be defined in .cpp files and the .h file provide extern "declarations" so that when compiling other .ino/.cpp files, the compiler is "aware" of them and can provide proper linkage.

the #ifdef DUMMYTABLE is the type thing done in .h files to prevent them from being processed multiple times since .h files can be included in other .h files.

it's convention to the following in .h files to avoid redundant processing. FILE_H will only be un-defined the first time the .h is processed and ignored subsequently because FILE_H gets defined the first time it is processed

I've got the:

#ifndef GameTableUtils_h
#define GameTableUtils_h

in the beginning, the #ifdef DUMMYTABLE serves another purpose.

The hint that variable declaration should not be in the .h file might help to resolve though.

Still strange that with 2 different #ifndef tags the compiler give 2 different results though....
I will do some more experimenting, but still feel something strange is going on.....

where is DUMMYTABLE defined?

DUMMYTABLE is defined in an c++ program on my Mac, so I'm simulating my program fist on my Mac using g++, see some code snippet below...

#define DUMMYTABLE					// Only define when testing on Mac
#define TABLEPIN			0		// GPIO pin of Led Strip
#define BUTTONCON1LEFTPIN	1		// GPIO pin of Console 1 left Button
#define BUTTONCON1LEFTCHAR 'a'		// Char dummy table of Consule 1 left Button

#include "Controller.h"
#include "TableButtons.h"
#include <stdio.h>
#include <termios.h> 
//see https://www.raspberrypi.org/forums/viewtopic.php?t=177157&start=25#p1130202
#include <unistd.h>

TableButtons *button = (TableButtons*)malloc(sizeof(TableButtons) * 4) ;
Controller thisController;

so just to add to that, DUMMYTABLE is not define in de Arduino IDE

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.