Question

I'm trying to get a custom robot to work with the Arduino IDE by declaring a new hardware "board" within the IDE. This contains all the mappings and whatnot, but there exists a library that I have access to the source.

Arduino IDE generates a cpp file as follows:

    #line 1 "cbotj.ino"
    #include "Arduino.h"
    void setup();
    void loop();
    #line 1
    void setup()
    {
         LED_open();
    }

    void loop()
    {

    }

I then get the error:

cbotj.cpp.o: In function `setup':
/Applications/cbotj.ino:3: undefined reference to `LED_open()'

Now in Arduino.h for the hardwares core I have

SUBSYS_OPENSTAT LED_open( void );

The function itself is implemented in another .c file within the core hardware functions (such as pinMode) and is completely valid as it works as a compiled library for other IDE's such as Atmel's AVR Studio 4 and 6.

Why am I getting the undefined reference? Headers are included, etc.

The hardware is selected as it works if I were to use things like pinMode on the pin for an LED works fine.

TIA

Was it helpful?

Solution

When linking to functions from a .c file in a C++ project (which seems to be what you have), you need to declare the function as extern "C".

Try changing the header files';

SUBSYS_OPENSTAT LED_open( void );

to

extern "C" {
    SUBSYS_OPENSTAT LED_open( void );
}

EDIT: Seems more than one file is confused about C++, try putting all your C includes inside extern "C" in your cpp file instead;

extern "C" {
  #include "Arduino.h"
}

void setup();
void loop();

void setup()
{
     LED_open();
}

void loop()
{

}

OTHER TIPS

Adding header files does nothing for you in terms of ensuring that your linker can find function definitions. That is, given a header file, the linker will not be able to hunt down the file with the definition (if it is different) on it's own. (If you forgot header files, you would get compilation errors along the lines of 'undefined symbol' or something.)

So you need to include the .cpp file in your project (the error arises from not having done that). You think you have (from the comments, just saw), but you probably haven't. :) In the comments you show the inclusion of a .c file; is a typo the issue here? Or the signatures of declaration and definition may inadvertently differ.

Edit: I think I've gotten this error as a result of circular header dependencies but I couldn't immediately create an example. It's extremely unlikely in this case anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top