SOLVED Compiling Arduino code outside of the IDE

Hi all,

I'm using the "Arduino make file" ( ::[ edam ]:: » Arduino Makefile ) along with AVR-GCC to compile Arduino code. Works fine.

So, I tried to change the source file extension from ".ino" to ".cpp" (and of course editing the makefile to allow it).

Strangely, the same source code (just a different extension) will not compile. The Arduino libraries and core code cannot be found. So to fix this I added "#include <Arduino.h>" to the source. Now it finds the core Arduino code, but says that "main" is redefined.

I'm completely confused. Why should the thing act differently simply because the file's extension is changed?

You'll probably have to post your example and your edited makefile to get an answer.

You might find it useful to look in the Arduino directory where you installed your IDE and follow the directory path:

hardware\arduino\avr\cores\arduino

You'll see main.cpp there which has the main() that starts the Arduino IDE compiles.

econjack:
You might find it useful to look in the Arduino directory where you installed your IDE and follow the directory path:

hardware\arduino\avr\cores\arduino

You'll see main.cpp there which has the main() that starts the Arduino IDE compiles.

Yes I know about main.cpp.

When I write Arduino code, I don't use [b]setup()[/b] and [b]loop()[/b], I use [b]int main (void)[/b].

My "code skeleton" looks like this:

int main (void)
{
    init ();
    Serial.begin (115200);
    // code goes here
    while (1);
}

This works... my "main" seems to "overcome" the one in "main.cpp".

Of course, I tried to temporarily "get rid of it" by renaming it to "main.cpx" - still didn't work. I got the same error - "redefinition of main".

I also searched inside every file within "arduino-1.0.6/hardware/arduino/cores/arduino" and found no other references to "main".

KenF:
You'll probably have to post your example and your edited makefile to get an answer.

I tried to put it in the post as code, but it's too large (> 9000 bytes) so it's attached as a ZIP file.

arduino.mk.zip (9.5 KB)

I found the problem...

This part:

[b]# automatically determine sources and targeet
TARGET := $(basename $(INOFILE))
SOURCES := $(INOFILE) \
    [color=red]$(wildcard *.c *.cc *.cpp *.C)[/color] \
    $(wildcard $(addprefix util/, *.c *.cc *.cpp *.C)) \
    $(wildcard $(addprefix utility/, *.c *.cc *.cpp *.C))[/b]

...was adding the original file in twice because it found "wildcard .cpp" as being my source file (of course) and that's why it worked when the file extension was ".ino" - because there was no wildcard for ".ino".

I then realized that there are no files in "util" or "utility" (don't even HAVE those directories), so changing the above to this:

[b]# automatically determine sources and targeet
TARGET := $(basename $(INOFILE))
SOURCES := $(INOFILE)

[/b]

...solved the whole problem!