Impossible to use the same variable in several .h files

Hello everyone, I just migrated my code on Mbed Studio and I realize that it is impossible to use a variable declared in a .h file in other .h files … I don’t understand, all my code work on Mbed Online, and no way to make more than 2 header…

I have created a test example: Files containing the variables:

#include "mbed.h"

enum testenum {
    toto,
    tata,
    titi
};

enum test2{
    a,
    b,
    c
};

int res = 0;

File containing the function, here testenum, a and res are not recognized!

#include "mbed.h"


void function_test (testenum en,int e)
    {
        int x,y;
        if(e == en){x = 1;}
        if(e == a){res = 1;}
        
    }

the main

#include "mbed.h"


#include "data.h"
#include "function.h"



// main() runs in its own thread in the OS
int main()
{
    while (true) 
    {
        function_test(toto, 1);    
    }
}


Thanks in advance for your help

Hello,

Unknown type name 'testenum' clang(unknown_typename)
Use of undeclared identifier 'a'  clang(undeclared_var_use)
Use of undeclared identifier 'res'  clang(undeclared_var_use)

there errors are reported by clang tool (Online compiler does not have it), probably because the expected/standart format is include the data.h into the function.h file. You can ignore it and the compilation will be ok, I think.

BR, Jan

Thank you for your answer,

The problem is that in the original code, it does not compile, this is just an example.
Mbed Studio refuses the interaction of several .h it seems…

I really don’t understand why Mbed Studio works worse than Online, did I forget to declare some things?

We agree that the compiler reads the header files one after the other and that if a variable is declared in the file n°1 it is declared for all project?
So in each following file I should be able to use it and have it recognized?

Thanks

The paradigm for what you want to do is to extern those shared variables in the common header, and then define them with the same signature in some .cpp file, and then anyone including the header ‘will see’ those definitions. Ergo:

// CommonData.h …

enum class TestEnumOne_t
{
toto,
tata,
titi
};

enum class TestEnumTwo_t
{
a,
b,
c
};

extern TestEnumOne_t testenum;
extern TestEnumTwo_t test2;

extern int res;

// CommonData.cpp …

TestEnumOne_t testenum = TestEnumOne_t::toto;
TestEnumTwo_t test2 = TestEnumTwo_t::a;

int res = 99;

// In any other file, .cpp or .h, that wants to use the common type variables…
#include “CommonData.h”

// Go ahead and use:
//
MBED_ASSERT(testenum == TestEnumOne_t::toto);
MBED_ASSERT(test2 == TestEnumTwo_t::a);
MBED_ASSERT(res == 99);

1 Like

I’m not sure but Online compiler probably uses different version of AC (ARM Compiler) compiler than MbedStudio = different rules and so on. That will be reason for different result.
BR, Jan

Yeah, I remember something about that, but I do not know how it can be affected by different compilers or compiler optimization.

BR, Jan

Johnny, Nuertey

Thank you very much for your analysis and examples.

I will test this and come back to you to tell you how it goes

Thanks a lot

1 Like