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

Adding all necessary .h files to every .c file

Hello there, I am already guessing that by the question alone it seems like what i'm asking is silly or wrong but I have no formal education here so I will need some help.

I have done all the tutorials posted, (advertising, services, characterisitcs, ble_uart, etc..) on my nrf51-dk but now I am on the ble_app_template to try and make my custom service and characteristics but I just can't seem to understand the structure of it all.

Why is it that main.c and the other tutorials .c files (like our_service.c) have so many .h files beneath it? are they all necessary or are some just to be sure? are these "packs" that are installed or are they something else? Whenever I make a .c file to make a service how do i get all the correct .h files the go beneath it so i can begin to follow the tutorials on how to create characteristics for my particular project?

So I guess im asking for an explanation as well as a little instruction. The nAN-36 is helpful but a its thing like this that i don't understand that are a big issue for me continuing. And answer is helpful, thank you

  • I am also a beginner in the field, but ads far as I understand the .h files are the header files which contain the prototypes for all the functions that are than defined in the .c files. Also I think u wont' be able to compile a project until u have the corresponding prototypes define in the .h files.

  • Yeah that's what I gathered as well. And I can't move any further till i do. Is there a way to import the files as a package or do i have to declare each one separately? Is there a model i can follow?

    I ultimately want to use the nrf51822 to send out pwm signals so is there specific header files needed?

    And thanks for the response!! @rbgautam

  • Each time you want to use a function that someone else has written, you need to #include a file where that function is defined. Otherwise, the compiler won't know which function you're talking about. In general, this means that if you're trying to use e.g. PWM code, you need to #include the relevant PWM header. If you're using code from lots of different modules, like main.c does, then you'll need a lot of header files.

    Code won't compile if you're missing a header file include, but it will compile if you have an extra header file included. This means that over time you may end up with extra #includes at the top of your file, as you may have added them to allow you to use functions you no longer use. So sometimes there can be non-essential #includes.

    I'm not sure what you mean by "import files as a package", or if there's a better way to answer your question. Is there another programming language that you know, so that I could make an analogy to that?

  • I learned on Java and i know some swift. I think i have a good idea on the concept and what you just said confirmed that, so thanks you for that! I just don't know which header files or how many I will need to accomplish what i need. Like in the services tutorial it called on about three, but one of those called on a few more. Were all those header files made specifically for that one .c class or was there just a general group of them that were imported as like a default package to back up creating a service?

  • For every external function you use in a file, you have to include the header where that function is defined. That's really it. There are no general groups or anything—you include what you need. If those in turn include headers, it's because they use functions in those other headers. You don't need to include those headers because they already do.

1 2