Arduino on Xcode Project — Official Thread

westfw:
Well, you could send out a supposedly complete project with no .pde file, for example.

right, similar to this:

When working in XCode, there is no reason to stick to the Arduino convention of naming the main .pde file the same as the project folder, so when going from XCode to Arduino IDE you'll need to re-name that main file.

the "no .pde" problem is as easily solved by renaming your main file. The Arduino IDE has no problem with having other .h and .cpp files in your project as far as I know.
And the syntax doesn't change so even if you share a project that has no .pde file in it, people can just as easily understand what you are doing.
I think you're right, whatever you do, there will be a tiny little compatibility problem but it it's still all C++ code...

On the other hand what is the benefit of working with pde files in XCode?

best,
tim.

avenue33:
Tim,
The templates on Xcode 4 are totally different from and incompatible with those on Xcode 3.

Yes, I know the templates are completely different now, I will try to look into that over the weekend when I might be able to steal my sister's macbook running Lion... :slight_smile:
But I thought the XCode projects were still compatible... no?

avenue33:
Building fails with the following error message:

    make

pic32-g++    -Wl,--gc-sections -mprocessor=32MX795F512L -Xlinker -T/Applications/Mpide.app/Contents/Resources/Java/hardware/pic32/cores/pic32/chipKIT-MAX32-application-32MX795F512L.ld -Xlinker -o/Users/avenue33/Desktop/Xcode_chipKIT/main.elf -Xlinker -M -Xlinker -Map=/Users/avenue33/Desktop/Xcode_chipKIT/main.map
/Applications/Mpide.app/Contents/Resources/Java/hardware/pic32/compiler/pic32-toavenue33/bin/../lib/gcc/pic32mx/4.5.1/../../../../pic32mx/lib/cpp-startup.o: In function _main_entry': (.text+0x10): undefined reference to main'
collect2: ld returned 1 exit status
make[1]: *** [/Users/avenue33/Desktop/Xcode_chipKIT/main.elf] Error 1
make: *** [build] Error 2
Command make failed with exit code 2

Wait, that is a completely different log, that can't be from the makefile in my XCode example

avenue33:
On top of that, Xcode doesn't recognise the symbols and doesn't jump to the definition.

I've had the same problem in XCode 3.2 The solution was to go to Project>Edit Project Settings>General and set "Project Format" to XCode3.2 compatible. Maybe try to set yours to XCode4 if this option still exists?

Otherwise, try rebuilding your codesense index... ?

avenue33:
Enclosed the culprit.

My template for chipKIT is broken and I don't know why :frowning:

I hope you'll be luckier than me :wink:

I think I will download your complete project from github and try it this weekend, maybe new eyes see something new :slight_smile:

best,
tim.

Bonjour Tim,

Sorry, I missed the most important:

#include libraries are compiled on Arduino UNO!

Thank you so much for your help :slight_smile: .

I pushed a commit to your repository suggesting taking avr/bin from within Arduino.app.

But for chipKIT, the Makefile no longer works.

I try to deal with both platforms.

Is there an Xcode 4 expert around?

avenue33:
Bonjour Tim,

Sorry, I missed the most important:

#include libraries are compiled on Arduino UNO!

Thank you so much for your help :slight_smile: .

I pushed a commit to your repository suggesting taking avr/bin from within Arduino.app.

But for chipKIT, the Makefile no longer works.

I try to deal with both platforms.

Is there an Xcode 4 expert around?

Hey great! so it is working now? With codesense etc?

I noticed your commit to github with the comment about using the avr-gcc from Arduino.app. I'm very new to GIT and github, so I had no clue how to import your adjustments... so I simply edited it in the makefile I was working on.
It was anyway already a completely different version than the one you branched from.
I just had one weird error with the inclusion of avrdude.conf but that was fixed easily and now it is working the way you suggested.

I'm going to try to become an XCode4 expert over the weekend.

ahoy!
tim.

References about Makefile

Short Tutorial on writing makefiles
Long Top (GNU make)

the "no .pde" problem is as easily solved by renaming your main file.

Easily solved by the typical XCode user. I don't think you're properly visualizing the level of cluelessness of the would-be consumer of a pre-made Arduino sketch. "rename main.cpp to foo.pde in a directory name foo, and put all the other file there too" just has SO many things that could go wrong.

On the other hand what is the benefit of working with pde files in XCode?

given a sketch containing .pde, .ino, .cpp, .c, and .S files, the .cpp, .c, and .S files are all compiled using standard c/c++/assembler conventions, while the .pde and .ino files are concatenated and (potentially) get special preprocessing and automatic #includes. Which is just a separate makefile rule, really, although the "preprocess" function doesn't exist. While there's no big reason not to make .pde files have their own #includes and prototypes so that the preprocessing isn't needed in an XCode environment, excluding the processing of .pde/.ino files from your Xcode processing makes SW developed there "gratuitously incompatible" with the Arduino environment.

It is (I claim) desirable that the XCode Arduino developer be able to pack his Arduino project into a .zip file and ship it to an Arduino IDE user, where it should compile without any changes. (I suppose that the zip file in question could be a separate XCode "target", but that seems to be unnecesarilly complicated.)

hmm there's some interesting stuff there...

Easily solved by the typical XCode user. I don't think you're properly visualizing the level of cluelessness of the would-be consumer of a pre-made Arduino sketch. "rename main.cpp to foo.pde in a directory name foo, and put all the other file there too" just has SO many things that could go wrong.

To me, it seems this point of view is a little bit overly cautious towards the "beginner Arduino User".
We were all beginners at a certain time and I think this little challenge of renaming a main.cpp file into a project_folder_name.pde file is only a small step to make you learn something new. Compared to learning about pointers, inheritance or even functions, renaming a file seems like a piece of cake. (mmm.. cake :-))

Before I started this effort to try to make an Arduino project with XCode, I had no knowledge at all about makefiles. Just by doing this I already learned so much about this completely new topic. It's fun! And it's a great way to learn too: trying to take this next step and looking through other people's code trying to make sense of it...
It's great that the Arduino IDE is working so good "out of the box" so beginners can get their hands dirty immediately. But at a certain point, if you want to create more complex programs you'll want to learn about classes, pointers, etc and then it is actually a good thing to have a more advanced software community that can happily program in their "professional" IDEs and assist those people.

But what if the Arduino IDE would allow correctly formatted C/C++ code? Then there would be no compatibility problem anymore, right?

I feel we're getting a little bit off topic here.. this thread was going to be about getting XCode to compile Arduino projects I believe.
So let's first get the templates ready for XCode 3.2.x and XCode 4.x and all the different boards, get them properly working for everybody and then worry about the potentially more complex problem of compatibility with the Arduino IDE.

If you have any suggestions on how to solve this problem, let's hear 'em! Maybe there is a very simple solution that I'm not seeing right now... like a simple text file included in the XCode template that explains what to modify for use in an Arduino IDE and vice versa?

best,
tim

Hi I've been playing around with this and can compile and upload sketches. Could you explain exactly how I can include a user-defined library with this template?

Suppose I have a library named foo located in the standard places:

~/Documents/Arduino/libraries/Foo/foo.h
~/Documents/Arduino/libraries/Foo/foo.cpp
~/Documents/Arduino/libraries/Foo/keywords.txt

how can I include it in the Xcode project?

Also, is it necessary to have the "ArduinoProgram" class with setup() and loop() methods, or can I write setup() and loop() functions in main.cpp?

Thanks much

Hi Professor,

That's a very good question. I suddenly realize that I've made this not so user friendly.
Basically, what you need to do is find the variables CPP_LIBRARIES, INCLUDE_LIBRARIES and CXXSRC and add some things.

I will update the github repository as soon as possible with a new makefile and build settings that allow you to add libraries of your own without needing to touch the makefile. I will report back here when that's uploaded to github.

Meanwhile, here is an example of how you could modify the makefile:
Make sure not to use ~/ for your home path, but the full name : /Users/yourUserName/ ... (because of a mistake I made)

#.. beginning of makefile...

# loading libraries
LIBRARIES	= EEPROM Ethernet/utility SPI \
Firmata LiquidCrystal Matrix Sprite \
SD SD/utility Servo SoftwareSerial \
Stepper Wire Wire/utility

# Here you add the paths to the folders that contains the cpp files of your libraries:
OTHER_LIBRARIES = /Users/yourName/Documents/arduino_sketchbook/library1 \ # << backslash means "continue on next line"
/Users/yourName/Documents/arduino_sketchbook/library2

# make the correct paths for the libraries
INCLUDE_LIBRARIES = $(addprefix -I$(LIB_ROOT)/,$(LIBRARIES)) 
INCLUDE_LIBRARIES += $(addprefix -I,$(OTHER_LIBRARIES)) #<<<<<<<<<<<<<<<<<<<<<<<<<<<< add this line

# find .cpp files in libraries
CPP_LIBRARIES = $(foreach lib,$(LIBRARIES),$(wildcard $(LIB_ROOT)/$(lib)/*.cpp))
CPP_LIBRARIES += $(foreach lib,$(OTHER_LIBRARIES),$(wildcard $(lib)/*.cpp))  #<<<<<<<<<<<<<<<<<<<<<<<<<<<< add this line

# main file
TARGET = main

# find .c files in arduino directory
SRC = $(wildcard $(ARDUINO)/*.c)

# .cpp files from libraries and arduino directory
CXXSRC = $(wildcard $(ARDUINO)/*.cpp) $(CPP_LIBRARIES) $(SOURCES) 
CXXSRC += $(wildcard $(OTHER_LIBRARIES)/*.cpp) #<<<<<<<<<<<<<<<<<<<<<<<<<<<< add this line


#.... rest of makefile...

You are right about ArduinoProgram, you don't really need it. It's only my preferred way of working.
Here's an example of main.cpp without the ArduinoProgram class:

#include "WProgram.h"

// Arduino doesn't know about new and delete, so let's teach 'm :
#include <stdlib.h> // for malloc and free
void* operator new(size_t size) { return malloc(size); }
void operator delete(void* ptr) { free(ptr); } 
// from: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230935955


// you'll need to add function prototypes now...
void setup();
void loop();


// functions

void setup(){
	Serial.begin(9600);
	Serial.println("this a test without ArduinoProgram");
	delay(100);
}

void loop(){
	Serial.println("we are looping...");
delay(100);
}


int main(void)
{
	init();
	setup();
	for (;;)
		loop();
	return 0;
}

I also have a question for you: What OS are you on and what board did you use to test?

Thanks for the feedback professor! Let me know if there is something else we can make better / easier to use...

best,
tim.

Thanks Tim - I'll try this out this evening.

I an running Snow Leopard (10.6.8) and loading to a Duemilanove.

Hi!

Things seem a more complicated with Xcode 4.2 running on Lion 10.7.2.

I had a look at Ino: maybe some ideas to grab?

I'm using 2 boards: Arduino UNO and chipKIT UNO32, with only one IDE Mpide 0022.

Hi Chaos & Avenue,

I have uploaded a new version of the XCode3.2 template to github. The adding of external libraries has been simplified a lot and it has (hopefully) clear instructions now in the README file. Get it here: GitHub - timknapen/Arduino-With-XCode: Developing on Arduino with XCode instead of the Arduino IDE and let me know what you think :slight_smile:

I have also made a working XCode 4 project in Lion, but it seems that making templates is still a little too obscure. I haven't done too much research yet (just installed Lion today) so it might not be as hard as it looks.

At the moment, I think it might be better to not go for a project template, but do it the way openFrameworks does it: creating a blank project that has everything setup and just copy the whole project folder to start a new Arduino project in XCode 4

Thanks for the info Chaos, I will check the box on Duemilanove :slight_smile:

What are .ino files?

best,
tim.

I've added a first version of an example XCode 4 project to the github repository.
Not a template, a blank project that you can copy and start from. When documentation on XCode 4 templates starts appearing I might have a go at it...

I'm not working in OSX Lion myself, so I'm going to wait for complaints/suggestions before I continue working on the XCode 4 project.

best,
tim.

Very cool Tim - I followed the instructions in the new Readme and was able to compile a sketch with the Streaming and Flash libraries under Xcode 3.2.6!

Thanks!

Congratulations Tim for your hard work!

Here's a minimal template for Xcode 4 you could use as inspiration.

http://blog.boreal-kiss.net/2011/03/11/a-minimal-project-template-for-xcode-4/
https://github.com/borealkiss/Minimal-Template

Best regards,

Thanks Avenue,

I had checked that blog post, but it doesn't really give a lot more information than what you can find in the templates that Apple provides with XCode.

For now, I'm going to wait until there is decent documentation on XCode4 templates. There are just too many things that we'd have to find by trial&error...

But if anyone is up for the task, don't let my laziness stop you! :slight_smile:

best,
tim.

ah that's great news, I'm glad it's starting to become useable!

best,
tim.

westfw:
What we "need" is a command-line mode of the Arduino app that does the Arduino-style preprocessing on a set of .pde (or .ino) files.
Having to figure out exactly what it does (slightly differently) with each Arduino release is a pain, and makes implementing Arduino program builds in other IDEs an exercise in chasing not-fully-documented behaviors. :frowning:

I fully agree. Learning Makefile is just horrendous.

Another big benefit is to ensure consistency of the code between the Arduino IDE and alternative IDEs.

So a command-line mode of the Arduino app should be great!

Some interesting info about command-line usage of Arduino.

Hello,
I've just tried Tim's Xcode project on Xcode 4.2.1 running on Lion *.

Trying to compile it i get

make: *** No rule to make target `/Applications/Arduino.app/Contents/Resources/Java/hardware/Sanguino/cores/arduino/pins_arduino.o', needed by `bin/core.a'.  Stop.
Command make failed with exit code 2

Any ideas how to solve this issue?

Thanks

  • EDIT: I'm using UNO.