Splitting a programm

19 Nov 2012

Hello

I have the problem to split my programm in more files. I dont exactly how that goes. I have allways a fault because my digital In/Outputs are multy declarated.

Have sombody and good explication and pherhaps a example file?

Thanks for Help Daniel

20 Nov 2012

Do you mean something like this?

Import programGlobalTest

Shows how to use an object from multiple .cpp files

21 Nov 2012

Not really i want a 2. .cpp file and a 2. .h file the in main oppen them. (My main program is too big so i want to split it in the diffrent parts.

21 Nov 2012

Suppose you want to split this program (main.cpp) into 2 parts:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

You could have one file (main.cpp) like this:

#include "mbed.h"

DigitalOut myled(LED1);

extern void FlashLed( void );

int main() {
    while(1) {
        FlashLed();
    }
}

and the other (Flash.cpp) like this:

#include "mbed.h"

extern DigitalOut myled;

void FlashLed( void ) {
    myled = 1;
    wait(0.2);
    myled = 0;
    wait(0.2);
}

This isn't great programming style, but it does work. A better style would be to use a header (.h) file containing a definition for FlashLed. The key thing here is the use of the keyword "extern". Essentially it means "I want you to know about a variable or function like this, but it isn't declared here, it is declared in another file". If you want your program to compile and link properly then everything you declare as extern (and you can do this in as many files as you like) must be declared somewhere else, once, and not as extern. Google "C extern" for more details.

21 Nov 2012

Thanks but wath is if i need in main.cpp also an led forexample myled. Then it give a fault of multideclaration.

21 Nov 2012

You can still access myled from either main.cpp or Flash.cpp without getting any multideclaration errors. For example this is fine:

#include "mbed.h"
 
DigitalOut myled(LED1);
 
extern void FlashLed( void );
 
int main() {
    while(1) {
        FlashLed();
        myled = 1;
    }
}

main.cpp knows where myled is declared because it is declared in main.cpp. Flash.cpp knows that myled is elsewhere because in Flash.cpp it is declared using extern.
Is it possible to post the sections of code that are causing problems?

22 Nov 2012

Thanks my fault was that i have more time written int 1= 2; and in the extern part extern int 1=2; And i dont have clean (LED1) in the extern part thanks for your help