Linking external .a file into project

I have a customer that wants to provide me with .a file for some confidential algorithm they want to run. I am tasked with implementing this into my firmware.

Using the links below I have been able to compile in the .a file along with its associated header file. I can even use the #include to include the header file. However, when I start to call functions from the header file, the ld.exe returns an undefined reference exception.

My file structure is below, as well as my CMakelists.txt. It appears that I have followed the links and the example for linking an external library perfectly. Can I get some support to what I am missing, or what I should try next?

Thanks

CMakeLists.txt:

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)

target_sources(app PRIVATE src/main.c)

###
set(insight_src_dir   ${CMAKE_CURRENT_SOURCE_DIR}/insight)

set(INSIGHT_LIB_DIR     ${insight_src_dir}/lib)
set(INSIGHT_INCLUDE_DIR ${insight_src_dir}/inc)

add_library(insight_lib STATIC IMPORTED GLOBAL)

set_target_properties(insight_lib PROPERTIES IMPORTED_LOCATION             ${INSIGHT_LIB_DIR}/insight.a)
set_target_properties(insight_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSIGHT_INCLUDE_DIR})

target_link_libraries(app PUBLIC insight_lib)
###

Here is my folder structure:

hello_world

  > build

  > insight

      - header.h

  > lib

      - library.a

  > src

      - main.c

  - CMakeLists.txt

  - prj.conf

Parents
  • Hello,

    Could you please try to include the library as shown in the edited CMakelists.txt file below and see if you get the same error? There are several ways you can link in pre-compiled libraries with CMake, and I'm not sure why your approach did not work. 

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    set(INSIGHT_LIB_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/insight)
    
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world)
    
    target_sources(app PRIVATE src/main.c)
    target_include_directories(app PUBLIC ${INSIGHT_LIB_DIR}/inc)
    
    zephyr_library_link_libraries(${INSIGHT_LIB_DIR}/lib/insight.a)
    

    Here is my folder structure:

    From your CMakelists.txt file it looks like 'lib' and 'inc' folder should be subdirectories in the 'insight' folder?

    Thanks,

    Vidar

  • Hello,

    You are correct, thanks for the fix - there is an inc and lib directory inside of the insight folder. This is the structure:

    hello_world
    
      > build
    > hello_world
        > insight
          > inc
            - header.h
          > lib
            - library.a
        > src
            - main.c
        - CMakeLists.txt
        - prj.conf

    I just tried your CMakeLists.txt file as-is and it resulted in the same error - this is what I see:

    Thanks for your timely response! I appreciate any other comments or feedback you have. I am double-checking with the customer but could it be the case that the .h does not exactly match up with the .a file?

    Thanks

  • Hello,

    No problem. Yes, maybe it could be that the header file does not match the library. You can try using 'nm' from the command line to confirm if the 'algorithm_init' function is defined in the library.

    'nm' is included in the toolchain at /ncs/toolchains/1f9b40e71a/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-nm:

    $ arm-zephyr-eabi-nm insight.a

  • Hi again,

    Thanks for these timely responses.

    I ran the nm executable and I see the algorithm_init() function but with a different set of parameters than described in the header file.

    I am going to be reaching out again for the correct file and then from there try this build again. Do you expect any further issues? I will let you know if I got any other issues but I appreciate the responses.

  • Hi,

    I think the CMakelists.txt is set up correctly as I've used the same functions to link in other libraries in the past, so I wouldn't expect any further issues. 

  • This can be closed I have been able to make significant progress on this. Thank you for your time.

Reply Children
Related