NVSL / PiDuino_Library

15 stars 4 forks source link

Including "Arduino.h" across multiple object files redefines 'main' #2

Open MichaelMGonzalez opened 7 years ago

MichaelMGonzalez commented 7 years ago

I tried compiling an existing Arduino library with the following commands

g++  -c at86rf2xx.cpp
g++  -c at86rf2xx-internal.cpp
g++  -c at86rf2xx-getset.cpp
g++ -x c++ -c arduino-at86rf233.ino
g++ -lpiduino -o test_radio at86rf2xx.o at86rf2xx-internal.o at86rf2xx-getset.o arduino-at86rf233.o

g++ gave me the following erros

at86rf2xx-internal.o: In function `main':
at86rf2xx-internal.cpp:(.text+0x0): multiple definition of `main'
at86rf2xx.o:at86rf2xx.cpp:(.text+0x0): first defined here
at86rf2xx-getset.o: In function `main':
at86rf2xx-getset.cpp:(.text+0x0): multiple definition of `main'
at86rf2xx.o:at86rf2xx.cpp:(.text+0x0): first defined here
arduino-at86rf233.o: In function `main':
arduino-at86rf233.ino:(.text+0x0): multiple definition of `main'

Inspecting the assembly showed that a main label was created for each object file

Within piduino's Arduino.h file, it seems to provide a full definition for main().

https://github.com/NVSL/PiDuino_Library/blob/master/Arduinoh/Arduino.h#L36

jgarzagu commented 7 years ago

If you have a main() function defined elsewhere or you want do define your own main() function you can include only the Piduino header. e.g.

#include "piDuino.h"

int ledPin = 4; // GPIO4

void setup() {
        pinMode(ledPin, OUTPUT);
}

void loop() {
        printf("LED ON\n");
        digitalWrite(ledPin, HIGH);
        delay(1000);
        printf("LED OFF\n");
        digitalWrite(ledPin, LOW);
        delay(1000);
}

int main () {
    setup();
    while(1){
        loop();
    }
    return (0);
}

You can check this webpage for more information: http://nvsl.github.io/PiDuino_Library/