簡體   English   中英

未定義的對弱函數的引用(靜態庫+ GCC)

[英]Undefined Reference to weak functions (Static Library + GCC)

大家好。 首先,我想向大家介紹我的問題!

我正在使用Atollic TrueStudio開發嵌入式系統。 在此系統中,我有一個驅動程序層(作為靜態庫構建。該庫基本上包含STM32 HAL,並且該庫包含許多弱函數)。 這些庫正在使用ARM GCC進行編譯,並且效果很好。 我可以將其鏈接到其他項目中,並且可以正常工作。

第二部分是問題,因為我想在項目中實施單元測試!

為此,我為同一驅動程序層靜態庫(帶有STM32 HAL代碼的庫)創建了另一種配置,但是這次我將使用MinGW對其進行編譯。 編譯有效,但是當我嘗試將此庫鏈接到另一個項目(可執行項目)時。 鏈接器返回“對...的未定義引用”(下圖)。

PS:問題出在鏈接器上,因為項目可以找到弱函數的聲明位置。 但是找不到它在哪里定義!

編輯

這是主要代碼

#include <MbedOS/platform/mbed_error.h>

int main(void)
{
   error("Stream obj failure, errno=%d\r\n",

   return 0;
}

這是在靜態庫內部定義函數的位置mbed_error.c

#include <MbedOS/platform/mbed_error.h>

static uint8_t error_in_progress = 0;

WEAK void error(const char* format,
                ...)
{

   // Prevent recursion if error is called again
   if (error_in_progress)
   {
      return;
   }

   error_in_progress = 1;

   #ifndef NDEBUG
      va_list arg;
      va_start(arg,
               format);
      mbed_error_vfprintf(format,
                          arg);
      va_end(arg);
   #endif

   exit(1);
}

這是mbed_error.h文件

#ifdef __cplusplus
extern "C"
{
#endif
void error(const char* format,
           ...);

#ifdef __cplusplus
}
#endif

這是來自控制台的結果

13:45:44 **** Build of configuration Debug for project testeLINKER ****
make all 
gcc -o "testeLINKER.elf"  ./src/main.o   -lSTM32F7_Driver_Layer -L"E:\TrueStudio_Projects\STM32F7_Driver_Layer\Projeto\TrueStudio\UnitTestLibrary"
./src/main.o: In function `main':
E:\TrueStudio_Projects\testeLINKER\Debug/../src/main.c:33: undefined reference to `error'
collect2.exe: error: ld returned 1 exit status
make: *** [testeLINKER.elf] Error 1

13:45:44 Build Finished (took 460ms)

編輯2隨后是靜態庫的nm輸出(僅是功能較弱的文件之一)

    C:\Program Files (x86)\Atollic\TrueSTUDIO for STM32 9.0.0\PCTools\bin>nm E:\TrueStudio_Projects\STM32F7_Driver_Layer\Projeto\TrueStudio\UnitTestLibrary\Source\MbedOS\platform\mbed_error.o
00000000 b .bss
00000000 d .data
00000000 N .debug_abbrev
00000000 N .debug_aranges
00000000 N .debug_info
00000000 N .debug_line
00000000 r .eh_frame
00000000 r .rdata$zzz
00000000 t .text
00000000 T .weak._error.
         w _error
00000000 b _error_in_progress
         U _exit
         U _mbed_error_vfprintf

對於每個可能和我一樣有疑問的人。 這就是答案。

問題是PE32文件格式不支持弱鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM