Arduino on Xcode Project — Official Thread

Hi,

I have been struggling to get Arduino to work with XCode for the longest time.
Finally, it is working. even with libraries ( but for some reason not the SD library... I'm not really sure why. )

I have been recycling makefiles that other people posted on the web, but none that I found could make libraries link correctly or even include extra .cpp files in my project's src folder.
So I have been adjusting and changing makefiles so many times I have no clue where my original source came from :-s

Here is the makefile that I'm using now for working with a Makerbot Sanguino based board:

ARDUINO = /Applications/Arduino.app/Contents/Resources/Java/hardware/Sanguino/cores/arduino
LIBRARIES = /Applications/Arduino.app/Contents/Resources/Java/libraries
AVRDUDE_DIR = /usr/local/CrossPack-AVR/bin
SIZE = $(AVRDUDE_DIR)/avr-size
SOURCES = $(wildcard src/*.cpp)

#loading libraries
STEPPER = $(LIBRARIES)/Stepper
SERVO = $(LIBRARIES)/Servo
EEPROM = $(LIBRARIES)/EEPROM
SD_UTIL = $(LIBRARIES)/SD/utility
INCLUDE_LIBRARIES = -I$(STEPPER) -I$(SERVO) -I$(EEPROM)
CPP_LIBRARIES = $(wildcard $(STEPPER)/*.cpp) $(wildcard $(SERVO)/*.cpp) \
$(wildcard $(EEPROM)/*.cpp) 

PORT = /dev/tty.usbserial*
TARGET = main
SRC = $(ARDUINO)/pins_arduino.c $(ARDUINO)/wiring.c \
$(ARDUINO)/wiring_analog.c $(ARDUINO)/wiring_digital.c \
$(ARDUINO)/wiring_pulse.c \
$(ARDUINO)/wiring_shift.c $(ARDUINO)/WInterrupts.c 
CXXSRC = $(ARDUINO)/HardwareSerial.cpp $(ARDUINO)/WMath.cpp $(ARDUINO)/Print.cpp \
$(CPP_LIBRARIES) $(SOURCES)
MCU = atmega644p
F_CPU = 16000000
FORMAT = ihex
UPLOAD_RATE = 38400

# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
DEBUG = stabs

OPT = s

# Place -D or -U options here
CDEFS = -DF_CPU=$(F_CPU)
CXXDEFS = -DF_CPU=$(F_CPU)

# Place -I options here : include libraries
CINCS = -I$(ARDUINO) $(INCLUDE_LIBRARIES)
CXXINCS = -I$(ARDUINO) $(INCLUDE_LIBRARIES)

# Compiler flag to set the C Standard level.
# c89   - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99   - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
CSTANDARD = -std=gnu99
CDEBUG = -g$(DEBUG)
CWARN = -Wall -Wstrict-prototypes
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -Wl,-gc-sections

CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING)
CXXFLAGS = $(CDEFS) $(CXXINCS) -O$(OPT) $(CTUNING)
LDFLAGS = -lm -Wl,-gc-sections -mmcu=$(MCU)

# Programming support using avrdude. Settings and variables.
AVRDUDE_PROGRAMMER = stk500v1
AVRDUDE_PORT = $(PORT)
AVRDUDE_WRITE_FLASH = -U flash:w:bin/$(TARGET).hex
AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \
  -b $(UPLOAD_RATE) -D
  

# Program settings
CC = $(AVRDUDE_DIR)/avr-gcc
CXX = $(AVRDUDE_DIR)/avr-g++
OBJCOPY = $(AVRDUDE_DIR)/avr-objcopy
OBJDUMP = $(AVRDUDE_DIR)/avr-objdump
AR  = $(AVRDUDE_DIR)/avr-ar
SIZE = $(AVRDUDE_DIR)/avr-size
NM = $(AVRDUDE_DIR)/avr-nm
AVRDUDE = $(AVRDUDE_DIR)/avrdude
REMOVE = rm -f
MV = mv -f

# Define all object files.
OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o)

# Define all listing files.
LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst)

# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)


# Default target.
all: sizebefore clean build  sizeafter

build: info	folder elf hex
		

elf: bin/$(TARGET).elf
hex: bin/$(TARGET).hex
eep: bin/$(TARGET).eep
lss: bin/$(TARGET).lss 
sym: bin/$(TARGET).sym

# print info
info:
	@echo "compiling sources: " $(SOURCES);
	@echo "compiling libraries sources: " $(CPP_LIBRARIES);
	@echo "----------";

# create the bin folder if it doesn't exist
folder:
	mkdir -p bin

# Program the device.  
upload: bin/$(TARGET).hex
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)


# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT) bin/$(TARGET).hex
ELFSIZE = $(SIZE)  bin/$(TARGET).elf
sizebefore:
	@echo "size before: ";
	@if [ -f bin/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi

sizeafter:
	@echo "size after:";
	@if [ -f bin/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(HEXSIZE); echo; fi



# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000 


coff: bin/$(TARGET).elf
	$(COFFCONVERT) -O coff-avr bin/$(TARGET).elf $(TARGET).cof


extcoff: $(TARGET).elf
	$(COFFCONVERT) -O coff-ext-avr bin/$(TARGET).elf $(TARGET).cof


.SUFFIXES: .elf .hex .eep .lss .sym

.elf.hex:
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

.elf.eep:
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

# Create extended listing file from ELF output file.
.elf.lss:
	$(OBJDUMP) -h -S $< > $@

# Create a symbol table from ELF output file.
.elf.sym:
	$(NM) -n $< > $@


bin/core.a: $(OBJ)
	@for i in $(OBJ); do echo $(AR) rcs bin/core.a $i; $(AR) rcs bin/core.a $i; done

# Link: create ELF output file from library.
bin/$(TARGET).elf: bin/core.a
	$(CC) $(ALL_CFLAGS) -o $@ src/$(TARGET).cpp -L. bin/core.a $(LDFLAGS)

# Compile: create object files from C++ source files.
.cpp.o:
	$(CXX) -c $(ALL_CXXFLAGS) $< -o $@ 

# Compile: create object files from C source files.
.c.o:
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 


# Compile: create assembler files from C source files.
.c.s:
	$(CC) -S $(ALL_CFLAGS) $< -o $@


# Assemble: create object files from assembler source files.
.S.o:
	$(CC) -c $(ALL_ASFLAGS) $< -o $@



# Target: clean project.
clean:
	$(REMOVE) bin/$(TARGET).hex bin/$(TARGET).eep bin/$(TARGET).cof bin/$(TARGET).elf \
	bin/$(TARGET).map bin/$(TARGET).sym bin/$(TARGET).lss bin/core.a \
	$(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d)

depend:
	if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
	then \
		sed -e '/^# DO NOT DELETE/,$d' $(MAKEFILE) > \
			$(MAKEFILE).$$ && \
		$(MV) $(MAKEFILE).$$ $(MAKEFILE); \
	fi
	echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
		>> $(MAKEFILE); \
	$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)

.PHONY:	all build elf hex eep lss sym program coff extcoff clean depend

I'm using this with XCode 3.2, I have attached a zip file of a working sample project.

hope this helps someone, I for one got a little bit balder trying to get this to work :slight_smile:

best,
tim.

Sanguino_xcode_test_5.zip (29.6 KB)

I have create a project on Github where I've put the XCode project templates:

With a little help from people who know about makefiles and compilers, I'm sure we can get decent XCode support for Arduino.

best,
tim.

Hi all,

I don't know if this is the right place for this, but I've updated my XCode projects on Github. It has now been updated to work with the UNO. I did a quick test and it seems to work fine.

Still only XCode 3.2 because I'm not running Lion yet..

go get it here: GitHub - timknapen/Arduino-With-XCode: Developing on Arduino with XCode instead of the Arduino IDE

best,
tim.

Hey anyone doing the same sort of thing,

Shouldn't we create a specific thread for Arduino + XCode (both XCode3.2 and XCode4) so that everybody who know how to write proper C++ can benefit from our efforts? It seems that all the information is now spread over multiple blogs, github repositories etc..

And even on the playgrond: Arduino Playground - DevelopmentTools there is no mention.

anyone?

best,
tim.

I fully agree with you.

I find here an extensive list of Arduino-related makefiles.

What we need:
• Actions: Build and Upload
• Boards choice: Arduino UNO, ... , chipKIT UNO32
#include libraries compiled and linked
• Xcode template

I could easily close my repository and point to yours.

Is there an Xcode specialist around? Please!

Best regards,

avenue33:
I fully agree with you.

I'll find here an extensive list of Arduino-related makefiles.

What we need:
• Actions: Build and Upload
• Boards choice: Arduino UNO, ... , chipKIT UNO32
#include libraries compiled and linked
• Xcode template

I could easily close my repository and point to yours.

Is there an Xcode specialist around? Please!

Best regards,

That list of makefiles is very interesting for learning, thanks!

I don't want to come in and take over the show, so I don't think you should close your repository, but a central place for working on this would be great. Like maybe a XCode + Arduino thread on this forum where we discuss the progress and one github repository where multiple people can work on this project.
I'm very new to git and github, so I don't really know how to do this. Do you?

I'm just starting to learn about makefiles so we could use someone that really is fluent with these thing. It would be great that people who download the templates don't have to go into the makefile and change a bunch of parameters just to add a library.
Right now I'm trying to have the choice of Board in the build settings of the project instead of in the makefile. I'm fairly close, but I can't find how to make a drowdown list in the build settings like the ones you have in a Cocoa project when selecting the "Base SDK"...

Avenue33, did you try my project template with XCode 4? I think you can just open it as a normal project with XCode4.
I don't have a computer that runs Lion yet, so I can't test this.

best,
tim

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 agree with this approach: all the parameters are on the files

/Applications/Mpide.app/Contents/Resources/Java/hardware/arduino/platforms.txt
/Applications/Mpide.app/Contents/Resources/Java/hardware/pic32/platforms.txt

The Arduino IDE or the chipKIT MPIDE are great tools. However, they are closed.

Both Arduino and chipKIT should disclose more so any standard IDE could be used: Visual Studio, Eclipse, Xcode, to name a few.

The Arduino IDE or the chipKIT MPIDE are great tools. However, they are closed.

They're not "closed." They are just rather tightly controlled. You can download the sources and make the patches to do what I described, but the chance that the patches will be accepted for inclusion in future releases is quite small.

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:

What do you mean with "preprocessing" exactly? In the XCode version, we are just using the Arduino C and Cpp libraries and avr-gcc to compile for the arduino, but no special preprocessing.

Or do you mean the converting from .pde files into .h and .cpp files that probably happens behind the screens in the Arduino IDE?

best,
tim.

do you mean the converting from .pde files into .h and .cpp files

Yep; that. Concatenate the .pde files, generate function prototypes, add the default .h files, and detect the used libraries. More or less. Subject to change.

westfw:

do you mean the converting from .pde files into .h and .cpp files

Yep; that. Concatenate the .pde files, generate function prototypes, add the default .h files, and detect the used libraries. More or less. Subject to change.

hmmm I'm not sure about that... I have absolutely no problem with writing good code when I get all the advantages in XCode.
Also, the code completion probably wouldn't work if you start working with .pde files without proper header files, right?

I think it would make more sense to have two types of working: the easy to learn and quick-sketch Arduino IDE way and a way for more experienced programmers to work with their preferred IDE and a good makefile.

best,
tim.

sure, as long as you don't end up creating sketches that can ONLY be compiled from the XCode environment. Assuming that you want to share your code, it'll need to compile from the Arduino IDE as well. I don't think that this has been a problem so far, but I can see it becoming one as programs get larger.

hey Westfw,

I think this won't be such a big problem. The things people share are more like snippets of code to do something specific and explain a concept or libraries, and they of course would work in any environment as they seem to be always made up of proper .h, .cpp and .c files.

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.

That is the only compatibility problem in the XCode -> Arduino IDE direction that I can think of.. The other direction could be a bit more involved because of the preprocessing, but still not a big deal.

I can't really imagine anything that would be workable in XCode (it's still the same compiler..) and would not work in the Arduino IDE.

best,
tim.

always made up of proper .h, .cpp and .c files.
:
I can't really imagine anything that would be workable in XCode and would not work in the Arduino IDE.

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

@Westfw

Feel free to join the project.

As the thread title stands, we plan to deal with Xcode.

Presently, the linker problem is solved and Xcode can compile sketches with #include libraries.

Now, next step is a template.

Tim,

timKnapen:
Avenue33, did you try my project template with XCode 4? I think you can just open it as a normal project with XCode4.
I don't have a computer that runs Lion yet, so I can't test this.

The templates on Xcode 4 are totally different from and incompatible with those on Xcode 3.

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

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

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:

2011-11-15 Makefile Arduino.txt (3.07 KB)

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?