c++ - 嵌入式 C/C++ : undefined reference of an existing symbol

标签 c++ c linker embedded

我很难将我的代码链接到 Atmel 库。

我的代码使用了在 Atmel 库中定义的函数 GetTickCount()。我的 cpp 文件编译正常但链接失败。该库在链接期间存在,并且实际上已被该过程中的另一个 C 文件使用。

  • 我的文件 I2C_due.cpp 调用函数 GetTickCount()

  • GetTickCount() 函数存在于库 libsam_sam3x8e_gcc_rel.a 中(来自 timetick.c)。这是 Atmel 的预建文件。

  • 文件 wiring.c(来自 Arduino)是用我的文件编译的,也调用了 GetTickCount(),但它放在 < strong>libFrameworkArduino.a 链接前。

在链接期间,链接器不会提示从 wiring.c 调用 GetTickCount(),而是处理我的文件。如果我从链接器命令行中删除 lib libsam_sam3x8e_gcc_rel.a,它当然也会提示 wiring.c 调用。所以我确定在链接期间正在使用 lib(它在命令行的末尾,因此链接器首先解析我的文件)。

我想知道两件事:

  1. 我在 C++ 方法中调用 C 函数。

  2. 与新的 C 可见性功能相关的内容。

GetTickCount()timetick.c 中定义,嵌入在 libsam_sam3x8e_gcc_rel.a 中:

extern uint32_t GetTickCount( void )
{
    return _dwTickCount ;
}

timetick.h:

extern uint32_t GetTickCount( void ) ;

链接器命令行:

arm-none-eabi-g++ -o .pioenvs/due/firmware.elf -Os -mthumb -mcpu=cortex-m3 \
  -Wl,--gc-sections -Wl,--check-sections -Wl,--unresolved-symbols=report-all \
  -Wl,--warn-common -Wl,--warn-section-align -Wl,--entry=Reset_Handler -u _sbrk \
  -u link -u _close -u _fstat -u _isatty -u _lseek -u _read -u _write -u _exit \
  -u kill -u _getpid -Wl,-T"flash.ld" (many objects).o \
  .pioenvs/due/src/Marlin/HAL/DUE/I2C_due.o (many objects).o -L(many lib dirs) \
  -Wl,--start-group .pioenvs/due/libFrameworkArduinoVariant.a \
  .pioenvs/due/libFrameworkArduino.a -lc -lgcc -lm -lsam_sam3x8e_gcc_rel \
  .pioenvs/due/lib/libWire.a .pioenvs/due/lib/libSPI.a -Wl,--end-group

错误:

/home/alex/(longdir)/HAL/DUE/I2C_due.cpp:239: undefined reference to `GetTickCount()'

...

.pioenvs/(longdir)/HAL/DUE/I2C_due.o:/home/alex/(longdir)/HAL/DUE/I2C_due.cpp:344: more undefined references to `GetTickCount()' follow

只需在库中检查一下:

$ nm -s libsam_sam3x8e_gcc_rel.a | grep GetTickCount
GetTickCount in timetick.o
00000001 T GetTickCount

关于如何链接我的文件的任何提示?

干杯。

亚历克斯。

最佳答案

分析 R.. 评论,我发现了我的错误。

不允许在 C++ 函数中调用 C 函数。 这就是我们在函数声明之前使用 extern "C" 的原因。

我必须创建一个 .c 和 .h 文件并在 .h 中使用 extern "C"

teste.c:

uint32_t MiliS()
{
    return GetTickCount();
}

teste.h:

extern "C" uint32_t MiliS();

这样我们就可以从 .cpp 文件中调用 MiliS(),它将成为 GetTickCount()

的包装器

关于c++ - 嵌入式 C/C++ : undefined reference of an existing symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371755/

相关文章:

c++ - 使用类方法时的空输出

c - 如何在c中检查特定字母是否被按下?

xcode - Apple Mach-O Librarian (libtool) 错误,没有特定错误

c - 从C中同一目录中的另一个文件调用函数

c++ - 如何管理std::vector的std::vector

c++ - 以下图像处理功能如何工作?

c++ - 将结构传递给函数时出现意外结果

c - Netcat shell 程序

c - 汇编 EAX 寄存器无故重置

c++ - 错误 LNK2019 : unresolved external symbol libcurl Visual studio