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"
#include "data.h"
#include "function.h"
// main() runs in its own thread in the OS
int main()
{
while (true)
{
function_test(toto, 1);
}
}
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.
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?
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:
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.