5
votes

I have a simple C library that looks like this:

//mycLib.h
#ifndef _MY_C_LIB_h
#define _MY_C_LIB_h                   
    typedef struct {char data1;
                    int data2;
                    } sampleStruct;             

    extern void mycLibInit(int importantParam);
    extern void mycLibDoStuff(char anotherParam);

    extern void sampleStruct mycLibGetStuff();
#endif



//mycLib.c
sampleStruct _sample;
void mycLibInit(int importantParam)
{
    //init stuff!
    //lets say _sample.data2 = importantParam
}

void mycLibDoStuff(char anotherParam)
{
    //do stuff!
    //lets say _sample.data1 = anotherParam
}

sampleStruct mycLibGetStuff()
{
    //return stuff, 
    // lets say return _sample;
}

It works well when called from other test software. However, as part of another project, I have to include it in an Arduino project and compile it to work on that platform as well. Unfortunately, When I run my Arduino code that looks like this:

#include <mycLib.h>

void setup()
{
  mycLibInit(0);
}

void loop()
{
}

I get the following compile error: code.cpp.o: In function setup': C:\Program Files (x86)\Arduino/code.ino:6: undefined reference tomycLibInit(int)'

I have read following threads on Arduino website:

but in all those cases the external library was in form of a c++ class with a constructor call in the Arduino code.

Is there a way to tell Arduino IDE that "hey this function is part of this C library" or, should I re-write my functionality into c++ classes? Its not my favorite solution because the same c-Module is being used in other projects. (I know I probably can use preprocessor directives to have the code in the same place but it is not a pretty solution!)

2
Arduino IDE is doing some funny (but totally not funny) stuff with the sources. It is rearranging them, including and messing in a totally weird way. So to the point: Arduino library should be a C++ class, residing in the Libraries directory under Arduino directory. This is the only way I could do it reliably..Or if it's just an .h file, it should be in the project directoryEugene Sh.
Thanks for the tip on having c++ class @EugeneSh. I will try the other trick and see if it will work.AleX_
This doesn't answer the question, but I would suggest ditching the Arduino “IDE” in favour of your own build process (e.g., makefile) - since you already know how to make your own libraries, it seems unlikely that you would benefit from the simplified IDE.Arkku

2 Answers

8
votes

You have to tell Arduino that your library uses C naming. You can use extern "C" directly in the Arduino code.

The next code compiles in Arduino IDE 1.05.

extern "C"{
  #include <mycLib.h>
}

void setup()
{
  mycLibInit(0);
}

void loop()
{
}

mycLib.h

#ifndef _MY_C_LIB_h
#define _MY_C_LIB_h

typedef struct {char data1;
                int data2;
                } sampleStruct;

  void mycLibInit(int importantParam);
  void mycLibDoStuff(char anotherParam);

  sampleStruct mycLibGetStuff();

#endif

mycLib.c:

#include "mycLib.h"
sampleStruct _sample;
void mycLibInit(int importantParam)
{
    //init stuff!
    //lets say _sample.data2 = importantParam
}

void mycLibDoStuff(char anotherParam)
{
    //do stuff!
    //lets say _sample.data1 = anotherParam
}

sampleStruct mycLibGetStuff()
{
    //return stuff, 
    // lets say return _sample;
}