如何将arduino库与标准C代码一起使用

The*_*uid 5 c c++ eclipse avr arduino

我正在使用Eclipse kepler进行AVR开发.我拥有的代码是C(开源),我已经调整它以便它完美运行.我的目标是ATmega2560,采用arduino mega2560的形式.使用arduino板严格用于硬件方便; 我们正在开发硬件作为定制板,其中包含大多数核心arduino mega2560组件.

我需要在这个项目中使用几个库,它们只能用作arduino库,即电子纸屏幕库(来自seeedstudio)和Nordic的BLE nRF8001.

如果我在eclipse中使用插件创建一个新的arduino项目,我可以完美地构建和运行arduino库的测试.

当我尝试将两个代码库合并在一起时,我似乎无法调用添加的arduino库中的函数 - 如果我调用它们,编译器会抛出链接错误.

Building target: Virgin2ManualArdInsert.elf
Invoking: AVR C Linker
avr-gcc -Wl,-Map,Virgin2ManualArdInsert.map -mmcu=atmega2560 -o "Virgin2ManualArdInsert.elf"         ./avr/adc.o ./avr/eeprom.o ./avr/lcd_and_input.o ./avr/main.o ./avr/strings.o ./avr/unimplemented.o ./avr/usart.o  ./aes.o ./baseconv.o ./bignum256.o ./ecdsa.o ./endian.o ./fft.o ./fix16.o ./hash.o ./hmac_sha512.o ./messages.pb.o ./p2sh_addr_gen.o ./pb_decode.o ./pb_encode.o ./pbkdf2.o ./prandom.o ./ripemd160.o ./sha256.o ./statistics.o ./stream_comm.o ./test_helpers.o ./transaction.o ./wallet.o ./xex.o   
./avr/main.o: In function `main':
main.c:(.text.startup.main+0xc): undefined reference to `writeEink'
collect2: error: ld returned 1 exit status
makefile:53: recipe for target 'Virgin2ManualArdInsert.elf' failed
make: *** [Virgin2ManualArdInsert.elf] Error 1
Run Code Online (Sandbox Code Playgroud)

作为测试,我只是尝试从main.c中调用eInk.cpp中的基本"写入显示"调用:

extern "C"{
void writeEink()
{

    EPAPER.begin(EPD_SIZE);                             // setup epaper, size
    EPAPER.setDirection(DIRNORMAL);                     // set display direction

    eSD.begin(EPD_SIZE);
    GT20L16.begin();

//    int timer1 = millis();
    EPAPER.drawString("testing", 10, 10);
    EPAPER.drawNumber(12345, 60, 40);
    EPAPER.drawFloat(-1.25, 2, 80, 65);
    EPAPER.display();                                   // use only once

}
Run Code Online (Sandbox Code Playgroud)

是一个从arduino核心构建的静态库的方式吗?我已经尝试过了(虽然看起来大部分程序已经过时)并且库不想链接/被调用.

在我的C代码中包含C++/Arduino调用的正确步骤是什么?我尝试过使用extern"C"{function()}; 在我的.cpp文件和.h文件中,但没有用.

感谢您提供任何帮助或指示我可以自己解决的问题.

Hei*_*ler 1

您可以尝试将 C 代码编译为 C++,只需将文件重命名为 *.CPP,但很可能您必须修改代码才能将其编译为 C++ 代码。有些事情对于 C 来说是允许的,但对于 C++ 来说是不允许的(比如调用未声明的函数)。

另一个解决方案是编写要从 C 使用的 C++ 函数的包装器。您必须考虑 C 相对于 C++ 的两个限制:

  1. C不是面向对象的
  2. C不支持函数重载

此示例Serial.print()展示了如何使用包装器处理此问题:

extern "C" void SerialPrintInteger( int value )
{
    Serial.print( value );
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,您将编写类似的函数SerialPrintFloat()SerialPrintString()例如 等extern "C"。前缀告诉编译器以可从 C 调用的方式创建该函数。