5

Please read this well to make sure you understand what I want to do.

  • I DO want Xcode to be able to compile, but only so I can debug in Xcode.
  • I do NOT want to use Xcode to compile or upload the code to the Arduino board. I will use the Arduino IDE in "Use external editor" mode instead.

What I have done (also as a future reference for people who might want to do the same thing):

  • In the project settings (click on the project file in the left pane)
  • I have changed the compiler to GCC to avoid many errors.
  • I have added the following paths to the Header Search Paths and Library Search Paths:

    • /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/lib/gcc/avr/4.3.2/include
    • /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include
    • /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino

(If you have installed Arduino.app somewhere other than the Applications folder, you will need to adjust the paths accordingly.)

In main.cpp, I have included <WProgram.h>, but it wasn't enough. I was getting undefined identifier errors (for SPCR, SPE, MSTR, SPR1, SPR0) due to not being able to pass the -mmcu=somechipname as a flag to the compiler, which caused no device to be defined and avr/io.h to be unable to include the file that defined those symbols. I got around it by manually including <avr/iom328p.h> which is the appropriate header file for my chip.

That's how far I got.

Now I get these errors:

Undefined symbols for architecture i386:
  "_init", referenced from:
      _main in main.o
  "_setup", referenced from:
      _main in main.o
  "_loop", referenced from:
      _main in main.o
  "_pinMode", referenced from:
      SBSetup()    in main.o
  "_digitalWrite", referenced from:
      SBSetup()    in main.o

The whole main.cpp including the evil offending code is this:

#include <WProgram.h>
#include <avr/iom328p.h>    // Getting around warning "device type not defined"
#define NumLEDs 25
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI

int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;

void SBSetup(void) {
    pinMode(datapin, OUTPUT);
    pinMode(latchpin, OUTPUT);
    pinMode(enablepin, OUTPUT);
    pinMode(clockpin, OUTPUT);
    SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
    digitalWrite(latchpin, LOW);
    digitalWrite(enablepin, LOW);
}

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

What do I about this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user922764
  • 61
  • 2
  • 5
  • Actually, someone just made me realize that the errors happen at linking time. So the code does compile, which is what I wanted for debug purposes. I'll leave this open anyway, since I can't self-answer the question until 8 hours from now and because it would be even better if those errors were gone. – user922764 Sep 01 '11 at 04:40
  • You won't be able to "Compile" anything with Xcode. However you'll be able to use Xcode IDE as your advance editor and set it up with an standalone GCC to compile/upload when you click *run* on Xcode, nothing else. You cannot attache LLDB to an Arduino for debugging. – Mojtaba Aug 30 '15 at 06:48
  • Also keep in mind that when you see this `Undefined symbols for architecture i386:` it means you are trying to compile AVR (or maybe ARM) code on i386 (x86 intel) machine, it just doesn't work. – Mojtaba Aug 30 '15 at 06:49

3 Answers3

3

These two great references let you set up XCode not only for compile, but also for upload:

  1. http://www.quietless.com/kitchen/setting-up-xcode-to-compile-upload-to-an-arduino-atmega328-duemilanove/
  2. https://github.com/timknapen/Arduino-With-XCode#readme

The first explains the additional tools you need, while the second provides a project template for XCode 4.2 (and previous versions).

I have used both and it works.

henning77
  • 3,824
  • 2
  • 26
  • 32
1

I think your best approach might be to forget about XCode, and just use the Arduino IDE.

For whatever it's worth, it doesn't even sound like you're building for the correct platform (i386?). Assuming you've got the AVR toolchain for GCC, you might have some luck looking at the default switches in the Arduino IDE's source:

src\Compiler.java

Good luck!

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 3
    No, I want to use Xcode because the Arduino IDE sucks. Seriously. There is no way, that I know of, to use avr-gcc with Xcode... and I don't think I need it given that the code compiled from Xcode will not be used for the arduino. – user922764 Sep 01 '11 at 03:56
  • 4
    Also, I posted this because I obviously don't want to use the Arduino IDE. Please don't tell me to just use it. – user922764 Sep 01 '11 at 04:02
  • I'm not telling you to "just use it". I'm saying that Xcode is fine for editing, compiling, linking and debugging your (Mac?) code; but you probably *can't* get very far trying to force XCode to edit, compile, link, download and debug Arduino code. For the Arduino part, you're probably best off just using the Arduino IDE. I was also saying that if you wanted to "force feed" XCode, you could probably get a few hints as to GCC compiler flags, etc from looking at Compiler.java IMHO... – paulsm4 Sep 01 '11 at 19:23
0

There are many people making XCode project templates for compiling and uploading from XCode.

I just added my version that works with including libraries, code completion etc.. but they only work as project templates in XCode 3.2

I think you can still open the project in XCode4 and use them as a guide to get around the errors you get..

You can find them here: https://github.com/timknapen/Arduino-With-XCode

jonsca
  • 10,218
  • 26
  • 54
  • 62
tim k
  • 162
  • 1
  • 9