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

Parents
  • 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?

Reply
  • 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?

Children
  • 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.

  • I mean i understand what they're for and why they exist, i just want to know how to get them into my new project. In this project i want to have a custom service with a few characteristics in it. I was wondering if I will i need to make my own header files for the .c class to create the service? The tutorial i followed had a service and its characteristic created in a .c file separate from the main.c, and this .c file had a lot of headers in order to accomplish its function of adding the service to the stack. I just assumed that the dozen or so headers that were added in the tutorial were not an excess and were standard for every project wishing to make a custom service and I wanted to know how to do it for y project.

    Sorry if i'm being confusing or difficult but this is the best way i know how to ask for help, thanks for your time

  • Just try it! You'll know if you need to include a header because it won't compile.

Related