Go Down

Topic: Right way to separate Servo code (Read 100 times) previous topic - next topic

uiguig

Hey there fellow programmers :)

I'm trying to break down the functions & objects related to the mechanical axes of a machine into separate .cpp/.h files (i'm breaking down a large sketch into a small one with external Cpp/h files actually).

To test this i've made a very simple test program.
My test program's filetree is as follows :




the library.h file #includes the Servo Library, creates a Servo instance and defines a prototype function to initialise it.

The library.cpp file #includes the library.h file and defines the initialisation function.

The .ino file, finally, #includes the library.h and tries to use the initialisation function.

However, when compiling I get the following error messages :

sketch/test_bug_arduino.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `servo0'
sketch/library.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.


I guess i'm not doing things right, but I don't know if it's rather due to wrong code architecture or just noob programming.

I have attached the test sketch in an archive.

If anyone can help.. Thanks :)

PaulS

Code: [Select]
#ifndef LIB_H
#define LIB_H

#include <Servo.h>

Servo servo0 ;

void servo_init();

#endif

ANY time this header file is included, an instance of the Servo class will be created.

Code: [Select]
#include "library.h"

void servo_init(){
    servo0.attach(4);
}

You included it here, so there's one instance.

Code: [Select]
#include "library.h"


void setup(){
    servo_init();
}

void loop(){
    // Nothing
}

And there's the other instance. So, the compiler is right. There are multiple instances. When you call servo_init(), which instance is it supposed to manipulate?
The art of getting good answers lies in asking good questions.

PaulS

By the way, that is ALL of your code, so zipping those three tiny files is inexcusable. Just post the code inline, in code tags, like I did.
The art of getting good answers lies in asking good questions.

uiguig

@PaulS, Thanks very much. I'm getting it now.

johnwasser

Maybe in the library.h put "extern Servo servo0" and in the library.cpp file put "Servo servo0;".  That should create the instance in library.cpp and allow the other modules that include library.h to reference that instance.

Or, if you want that "servo0" to only be known to the library, move it from library.h to library.cpp and maybe make it "static Servo servo0;" so it is known only to library.cpp.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

uiguig

johnwasser, thanks for the info. I think hadn't understood what static does, so I went back to reading :) Looks like that's what I've been looking for !

PaulS, by the way I attached the files as a zip archive because I thought it would make it easier to inspect on the other side rather than copy-pasting code. I'll remember for next time.

Go Up