如何在arduino中使用标准的c头文件

Ale*_*eX_ 5 c arduino header-files

我有一个简单的C库,看起来像这样:

//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;
}
Run Code Online (Sandbox Code Playgroud)

从其他测试软件调用时效果很好.但是,作为另一个项目的一部分,我必须将它包含在Arduino项目中,并将其编译为在该平台上工作.不幸的是,当我运行看起来像这样的Arduino代码时:

#include <mycLib.h>

void setup()
{
  mycLibInit(0);
}

void loop()
{
}
Run Code Online (Sandbox Code Playgroud)

我得到以下编译错误:code.cpp.o:在函数setup': C:\Program Files (x86)\Arduino/code.ino:6: undefined reference tomycLibInit(int)'

我在Arduino网站上阅读了以下主题:

但在所有这些情况下,外部库是一个c ++类的形式,在Arduino代码中有一个构造函数调用.

有没有办法告诉Arduino IDE"嘿这个函数是这个C库的一部分",或者,我应该将我的功能重写到c ++类中吗?它不是我最喜欢的解决方案,因为在其他项目中使用了相同的c-Module.(我知道我可能会使用预处理程序指令将代码放在同一个地方,但它不是一个很好的解决方案!)

cre*_*ndo 8

您必须告诉Arduino您的库使用C命名.您可以extern "C" 直接在Arduino代码中使用.

下一个代码在Arduino IDE 1.05中编译.

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

void setup()
{
  mycLibInit(0);
}

void loop()
{
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)