Search code examples
clinkerstatic-librariesweak-references

Can't replace __weak function in static library, which wrapped in another static library with strong definition


Context: IAR ARM Compiler.

I'm trying to wrap hardware library ("STM32 HAL library") into library with my additions and link it to my executable.

As in scheme:

   /platform
    - /hal_library
    -  -  hal_source.c (with weak foo())
    -  hal_portable_source.c (with strong foo())
    -  platform.c (with strong foo())
   main.c

CMake has this structure (pseudo code):

    ### Hal_Library.cmake
    
    add_library(hal_lib ${HAL_SOURCES})
    target_include_directories(hal_lib ${HAL_HEADERS}

    ### Platform_Library.cmake

    include(Hal_Library.cmake)
    add_library(platform_lib ${PLATFORM_SOURCES})
    target_include_directories(platform_lib${PLATFORM_HEADERS}
    target_link_libraries(platform_lib hal_lib)

    ### CMakeLists.txt

    add_subdirectory(./platform)
    add_executable(my_app main.c)
    target_link_libraries(my_app platform_lib )

hal_source.c contain __weak void Foo(void) AND! calls it in self functions. Vendor assume what user replace it with strong implementation void Foo(void) in his code. And it is works, when you add strong_foo_realization.c in executable directly.

But I'm trying to replace it in platform_lib with strong_foo_realization.c added and wherehal_lib is linked to.

And I get, what __weak Foo() is not replaced by mine strong Foo().

Referring to *.map file, the strong_foo_realization.c is never been added.

I've read:

__attribute__((weak)) and static libraries

and other topics, but seems there is no answer to my situation.

I've also tried:

  • --whole_archive did not help
  • Add all sources from hal_lib to platform_lib (and remove hal_lib at all). It didn't help
  • Change the position of hal_portable_source.c.o in link command. It didn't help
  • Same for library.

Solution

  • It is compiler problem. IAR dont works the way GCC does. There are no info how to make it work with IAR compiler.