Inclusion of user-defined header file in Connect SDK library files

Hello, I have a need to include a header file that I have defined in one of the Connect SDK .c files. For the purposes of this question, let's say I want to #include "myfunctionsandvariables.h" in azure_iot_hub.c. An example of my file structure in my linux home directory is below.

-ncs
     -bootloader
     -mbedtls
     -modules
     -nrf
     -nrfxlib
     -test
     -tools
     -zephyr
     -.west

-src
     -myproject
          -Kconfig
          -include
               -myfunctionsandvariables.h
          -src
               -main.c
               -myfunctionsandvariables.c
          -myboard            
            
   -build
               -boardoverlays
                    -myboardoverlay.overlay
               -prj.conf
               -CMakeLists.txt
               -Kconfig

I am struggling to configure my CMakeLists.txt files so that this header file can be included in (for example) azure_iot_hub.c. The files in "myproject" are configured so that I can include myfunctionsandvariables.h at the top of main.c, then and build without issue (if I use an unchanged azure_iot_hub.c).

I have attempted to add the following line to ~/ncs/nrf/CMakeLists.txt, and several similar permutations with no success. The error that I get when building is "fatal error: myfunctionsandvariables.h: No such file or directory"

zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)

Could you please give any guidance on how to go about accomplishing this?

  • Update - I have tried zephyr's external_lib sample and it works fine. However, when I use that example to build myfunctionsandvariables as an external library, I receive the following error:

    /home/myuser/ncs/zephyr/modules/hal_nordic/nrfx/./nrfx_glue.h:297:10: fatal error: ../src/nrf_802154_peripherals_nrf52.h: No such file or directory
      297 | #include <../src/nrf_802154_peripherals_nrf52.h>
          |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    This error seems to be the result of my "#include <zephyr.h>" statement in myfunctionsandvariables.h.

    Is this the path that I should be going down to solve my problem? Will building myfunctionsandvariables as an external library allow me to #include it in the ConnectSDK libraries?

  • Hi Brown,

    Are you actually trying to create an "external static library", or are you simply trying to create some header and source files for your project?

    To include files according to the way you described your directory structure, you can update your cmakelists.txt with this:

    target_sources(app PRIVATE
        src/main.c
        src/mylib.c
    )
    
    include_directories(
        include
    )

    and then add

    #include "mylib.h"
    

    in your main.c.

    and then place your files like this:

    I have created a zip files of the hello world sample modified to call hello world from a library:

    hello_world_library.zip

    Does this answer your question?

    Best regards,

    Raoul

  • Hi Raoul,

    Like you did, I included the "target_sources" and "include_directories" lines in my src/myproject/myboard/Cmakelists.txt and the "#include "myfunctionsandvariables.h"" line in my main.c. I should have clarified this in my original question. My file structure up above seems to match the structure in your picture, which is good.

    Let's say I define "void myfunc(void)" in myfunctionsandvariables.h and myfunctionsandvariables.c. I can successfully call myfunc() in main.c, but I cannot call this function in something like the Connect SDK library's azure_iot_hub.c or aws_iot.c.

    How could I call myfunc() in azure_iot_hub.c or aws_iot.c?

    Thanks.

Related