Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 4 months ago.
How to use a variable on main file on another file
The following program is running OK <<
- include "mbed.h"
DigitalOut myled(LED1); DigitalOut TM_STB(PA_0); DigitalOut TM_CLK(PA_1); DigitalOut TM_DIO(PA_4); int main() { while(1) { myled = 1; LED is ON TM_STB = 1; TM_CLK= 1; TM_DIO = 1; wait(0.02); 200 ms myled = 0; LED is OFF TM_STB = 0; TM_CLK= 0; TM_DIO = 0; wait(0.020); 1 sec } }
Then I added a second line on the main program
- include "file1.h"
and create 2 files file.h and file1.c file1.c
- include "mbed.h" void Liga() { TM_STB = 1; } ____ file1.h
void Liga() ;
Then I get a lot of errors Error: Identifier "class" is undefined in "extras/mbed_f9eeca106725/platform.h", Line: 36, Col: 2 Error: Expected a ";" in "extras/mbed_f9eeca106725/platform.h", Line: 36, Col: 22 and many other
What I did wrong ?
1 Answer
8 years, 4 months ago.
The first thing you did wrong was skip the <<code>>
tags. Please see the editing tips for how to format code.
main.c
#include "mbed.h" #include "file1.h" DigitalOut myled(LED1); DigitalOut TM_STB(PA_0); DigitalOut TM_CLK(PA_1); DigitalOut TM_DIO(PA_4); int main() { while(1) { myled = 1; TM_STB = 1; TM_CLK= 1; TM_DIO = 1; wait(0.02); myled = 0; TM_STB = 0; TM_CLK= 0; TM_DIO = 0; wait(0.020); } }
file1.c
#include "mbed.h" void Liga() { TM_STB = 1; }
file1.h
void Liga() ;
On to the actual problem...
There are some clear bugs with your code, I would have expected them to generate a different sort of error (the one you are getting is normally a missing } or ; in your header file but I can't see one missing) but they need to be fixed so do that and then see if the error goes away. If not please post the exact code used in a way that's readable..
Firstly you need to include file1.h in file1.c, just because they have the same name doesn't mean the compiler knows they are related.
Secondly you need to protect file1.h from getting included twice in the same .c file. With such a simple situation that isn't an issue, this generally is caused by another .h file that is used also including it. However it's good practice to add this to all headers so that it doesn't come back and bite you later.
Finally you need to declare the variable you want to share between the c files in the header. You can't actually define the pin in the header, if you did that it would end up being defined twice, once in each .c file. What you do is use the extern directive to tell the compiler that some .c file somewhere will define a digitalOut called TM_STB and so assume that it exists. Since DigitalOut is in the mbed library we also need to include the mbed library in the header or we could end up with code that is dependent upon the order of the #includes in a .c file. That's not a situation you want to be in.
file1.c
#include "mbed.h" #include "file1.h" // <- need to include this header here too. void Liga() { TM_STB = 1; }
file1.h
// standard method to prevent header from getting included twice #ifndef __file1_h__ #define __file1_h__ #include "mbed.h" extern DigitalOut TM_STB; // tell anything including this that TM_STB exists but is defined elsewhere void Liga() ; #endif
UPDATE
Fixed typo on file1.h pre-compiler directives.
Also as noted below file1 should be .cpp not .c otherwise including the mbed library (which is c++) creates compiler errors.
Did all changes and problem still there didn't compile
main.c
#include "mbed.h" #include "file1.h" DigitalOut myled(LED1); DigitalOut TMSTB(PA_0); DigitalOut TMCLK(PA_1); DigitalOut TMDIO(PA_4); int main() { while(1) { myled = 1; // LED is ON TMSTB = 1; TMCLK= 1; TMDIO = 1; wait(0.02); // 200 ms myled = 0; // LED is OFF TMSTB = 0; TMCLK= 0; TMDIO = 0; wait(0.020); // 1 sec } }
file1.c
#include "mbed.h" #include "file1.h" void Liga() { TMSTB = 1; }
file1.h
#ifndef __file1_h__ #define __file1_h__ extern DigitalOut TMSTB; void Liga() ; #endif
Errors
Files /media/uploads/ebclr/nucleo_tmxx_zip_nucleo_l152re_-1-.zip
posted by 27 Jul 2016Two changes to make it all work:
Firstly my mistake, I made a typo, in file1.h the first two lines need to be consistent. One line has file1_h, the other has file_1_h. Sorry. I'll fix the answer above so that it's correct for others.
Secondly, change file1.c to being called file1.cpp and the errors go away.
The mbed library is c++, if you include it in a .c file the compiler tries to treat it as c and gets confused. The code in the file can be pure c if you want but if it includes the mbed library either directly or via some other header file then it needs to be .cpp
posted by 27 Jul 2016