cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] Undefined Reference to custom Static Library

JNoon.1
Associate III

Hi,

With supply issues currently, I have been working on 3 different micro controllers of the same family, STM32F4xx. I am trying to contain the hardware specific code in their own projects, 3 each for the processors. But I have common code that is used for all 3.

Now to solve this I have created a custom STM32 project that is setup to be a static library. This will contain all the common code. This builds fine and outputs a .a file, and the main project links to the library okay. But the problem comes during the linking stage and gives me undefined references to functions in the library.

In the main project settings -> C/C++ Build -> settings and in the Tool Setting tab, under MCU GCC Linker -> Libraries I have linked the library to allow the .a file to be found and linked to.

I was wondering if anyone else has been able to successfully create a static library and compile and link to it without having any undefined references?

1 ACCEPTED SOLUTION

Accepted Solutions
JNoon.1
Associate III

I had lots of errors like this:

c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: C:/Playground/AppA/Debug/../Core/Src/main.c:567: undefined reference to `HAL_GPIO_Init'

My linker command is this:

-mcpu=cortex-m4 -T"C:\Playground\AppA\STM32L431KCUX_FLASH.ld" --specs=nosys.specs -Wl,-Map="${BuildArtifactFileBaseName}.map" -Wl,--gc-sections -static -L"C:\Playground\LibA\Debug" -Wl,--start-group -lLibA -lc -lm -Wl,--end-group --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb

The problem is my AppA would give undefined references to LibA, which has the Drivers folder that STM32CubeIDE generates. This is so I could have multiple Apps which use different processors and link them to the same LibA.

I did find the solution to this problem, LibA when it gets generated by STM32CubeIDE it does not get generated with the Src folder inside Drivers/STM32L4xx_HAL_Driver for some reason, that it why it gave an undefined reference as there were no .o files for those Src files, so the Library was not including them in the .a output. Those files had the functions in that the Application was trying to use. Not the best way but I copied the Src folder into the correct place from another project and it linked fine once there were .o files to use.

View solution in original post

3 REPLIES 3
KnarfB
Principal III

Should work, try a minimal example with STM32CubeIDE generated code.

How are you compiling and linking (options?) and what is the console output? What exactly are the error messages?

Common issues: linking C and C++ together, functions not defined with external linkage, ...

There are tools to list exported symbols like arm-none-eabi-objdump. You find them next to the arm-none-eabi-gcc compiler.

hth

KnarfB

> the main project links to the library okay. But the problem comes during the linking stage

So is it linking okay or not?

Post the error message(s) with explanation.

Is there only one library? Isn't the problem in linking within functions which are in the library?

Libraries are linked in a different way than objects - while symbols in objects are looked up recursively, symbols in libraries are walked through only once (unless linker is explicitly told to do otherwise). That's why placement of individual objects within library matters, and that's why order of linking of libraries matters.

https://sourceware.org/binutils/docs/ld/Options.html#index-groups-of-archives

JW

JNoon.1
Associate III

I had lots of errors like this:

c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: C:/Playground/AppA/Debug/../Core/Src/main.c:567: undefined reference to `HAL_GPIO_Init'

My linker command is this:

-mcpu=cortex-m4 -T"C:\Playground\AppA\STM32L431KCUX_FLASH.ld" --specs=nosys.specs -Wl,-Map="${BuildArtifactFileBaseName}.map" -Wl,--gc-sections -static -L"C:\Playground\LibA\Debug" -Wl,--start-group -lLibA -lc -lm -Wl,--end-group --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb

The problem is my AppA would give undefined references to LibA, which has the Drivers folder that STM32CubeIDE generates. This is so I could have multiple Apps which use different processors and link them to the same LibA.

I did find the solution to this problem, LibA when it gets generated by STM32CubeIDE it does not get generated with the Src folder inside Drivers/STM32L4xx_HAL_Driver for some reason, that it why it gave an undefined reference as there were no .o files for those Src files, so the Library was not including them in the .a output. Those files had the functions in that the Application was trying to use. Not the best way but I copied the Src folder into the correct place from another project and it linked fine once there were .o files to use.