This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Adding header and source files to nRF Connect SDK Project

Hello all,

I am working on a custom application that requires gps, https post requests, SPI, and CAN, and I am having trouble adding source and header files to my project. Following the tutorial on the nRF Connect SDK documentation page, I am trying to add a new file to CMakeLists.txt by right clicking on Project 'app/libapp.a' , but sometimes the option to add a new file isn't available. Depending on which project you start with, there is no option to add new files to Cmake, which is strange.

I eventually found an example that allowed me to actually add files to CMake (at_client), but am now getting multiple errors and multiple stages of building. How can I add .h and .c files to CMakeLists.txt?

I have searched through devzone looking at examples but don't see a clear step by step manual for adding .c and .h files to an nRF Connect SDK project. Some examples say to add to the 'src' folder, but nowhere do I see a src folder. On most other IDEs I just create new folders for my header and source files and just add them there, but this doesn't seem to link correctly. Any help would be appreciated, thank you.

-Kyle Garland

Parents
  • Hi, Kyle!

    This is unfortunate behavior. I will take further internally. In the meantime I recommend doing the addition of header and source files manually by:
    1. Navigating to the project folder either using the file explorer or a terminal. 
    2. Placing your .c files in the src folder located there.
    3. Making an "include" folder where you place your header files.
    4. Edit CMakeList.txt directly as described in our sample modification guide.

    Note that you may need to reload your SES project after changing CMakeLists.txt.

    Hope this helps!

    Best regards,
    Carl Richard

  • Hi Carl,

    Thank you for your response! I found the src folder in the project directory and that seems to do the trick. Would I need to then do something in CMakeList.txt like:

    zephyr_include_directories(include)
    zephyr_library_include_directories(include)

    add_subdirectory(src/https)

    add_subdirectory(src/gps)

    add_subdirectory(src/device_drivers)

    And then after, would I need to add dependencies for each specific project? Like If I'm starting with https_client and want to add the gps code, do I need to change the project dependencies to include the gps project.

  • Hi again, Kyle!

    Good to hear. CMakeLists must indeed be updated as you have shown here, but note that the Zephyr documentation recommends that both source and header files are located under the src directory. You can have a look at this small hello world sample I've put together here:  8540.hello_world.zip. What's worth noting is that every subdirectory of src should contain it's own CMakeLists targeting the source code of that module. You can see this in the sample I've added here. 

    You should not need to do anything else than updating CMakeLists according to your project contents, and updating your #includes. 

    Hope this makes sense!

    Best regards,
    Carl Richard

  • Hi Carl,

    This is great, and makes perfect sense! thanks for the example.

    Sincerely,

    Kyle Garland

Reply Children
No Data
Related