【发布时间】:2015-03-05 20:19:06
【问题描述】:
我有一个简单的 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;
}
从其他测试软件调用时效果很好。但是,作为另一个项目的一部分,我必须将它包含在一个 Arduino 项目中并编译它以在该平台上工作。不幸的是,当我运行我的 Arduino 代码时,如下所示:
#include <mycLib.h>
void setup()
{
mycLibInit(0);
}
void loop()
{
}
我收到以下编译错误:
code.cpp.o:在函数setup':
C:\Program Files (x86)\Arduino/code.ino:6: undefined reference tomycLibInit(int)'
我在 Arduino 网站上阅读了以下主题:
- http://www.arduino.cc/en/hacking/libraries
- http://playground.arduino.cc/Code/Library
- http://forum.arduino.cc/index.php?topic=37371.0
- http://arduino.cc/en/Hacking/BuildProcess
但在所有这些情况下,外部库都是 c++ 类的形式,并在 Arduino 代码中调用构造函数。
有没有办法告诉 Arduino IDE “嘿,这个函数是这个 C 库的一部分”,或者,我应该将我的功能重新写入 c++ 类吗?它不是我最喜欢的解决方案,因为相同的 c-Module 正在其他项目中使用。 (我知道我可能可以使用预处理器指令将代码放在同一个地方,但这不是一个很好的解决方案!)
【问题讨论】:
-
Arduino IDE 正在对源代码进行一些有趣(但完全不有趣)的事情。它正在重新排列它们,包括并以一种完全奇怪的方式搞乱。所以重点是:Arduino 库应该是一个 C++ 类,位于 Arduino 目录下的 Libraries 目录中。这是我可以可靠地做到这一点的唯一方法。或者如果它只是一个
.h文件,它应该在项目目录中 -
感谢 @EugeneSh 提供有关拥有 c++ 类的提示。我会尝试另一个技巧,看看它是否有效。
-
这不能回答问题,但我建议放弃 Arduino “IDE”,转而使用您自己的构建过程(例如,makefile)——因为您已经知道如何制作自己的库,您似乎不太可能从简化的 IDE 中受益。
标签: c arduino header-files