gpt4 book ai didi

c++ - 我如何使用带有标准 C 代码的 arduino 库

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:09 27 4
gpt4 key购买 nike

我正在使用 Eclipse kepler 进行 AVR 开发。我拥有的代码是 C(开放源代码),我已经对其进行了调整,因此可以完美运行。我的目标是 ATmega2560,采用 arduino mega2560 的形式。使用 arduino 开发板完全是为了硬件方便;我们正在开发硬件,使其成为具有大部分核心 arduino mega2560 组件的定制板。

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

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

当我尝试将 2 个代码库合并在一起时,我似乎无法调用添加的 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

作为测试,我只是尝试从 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

}

从 arduino 内核构建的静态库是否适合此处的方式?我已经试过了(虽然看起来大多数程序都已经过时了)并且库不想链接/被调用。

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

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

最佳答案

您可以通过简单地将文件重命名为 *.CPP 来尝试将您的 C 代码编译为 C++,但很可能您必须修改您的代码以使其编译为 C++ 代码。有些事情 C 允许,但 C++ 不允许(比如调用未声明的函数)。

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

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

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

extern "C" void SerialPrintInteger( int value )
{
Serial.print( value );
}

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

关于c++ - 我如何使用带有标准 C 代码的 arduino 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com