I'm working on documentation and combining 2 pieces of code for testing parts of a larger project.

I'm supposed to comment and make the code very clear to people who will use it later and make sure it's modular. I'm supposed to make header files, to reference function chunks in the main code from just the #include(s).

I understand in C++, that the convention is that for each header files(?), .h, there is a .cpp, where the .h is the header file that declares all the content, and that the .cpp is where you define the .h, ie. implementation.

My supervisor told me to put more parts of the code into library files, because additional components will be added in the future -

but confused because all the length comes from using already existing library files, just a specific pattern of calling pre-defined functions.

If I put the files into a .h, how do I reference all the calls? Make a void setup () {}?

The compiled code is 348 lines right now. All it has are some #include(s), code to calibrate and read from 2 different sensors using I2C and SPI protocols, with 1 shared master an Adafruit Bluefruit nRF52832, and comments with links to datasheets.

The bulkiness is in the set-ups and the different ways to represent data (have 24 lines just to print out data from 1 sensor).

Should the entire code be just calling built in functions from a library file? So people who read the code don't see the details so much?

1 Answer 1


It's very subjective what should and shouldn't be put in a library.

I use the simple rules:

  • If it's an atomic piece of code like a hardware driver, it's a library.
  • If it's something that could be used in multiple sketches, it's a library.

Maybe things like configuration options could be put in a header file (not a library - a header file in the sketch folder).

You could maybe split portions of your sketch into C++ files or INO files within the sketch (not a library - files in the sketch folder, and using INO files is easier since you don't need header files, the IDE does it all for you) to group things logically together and make it easier to manage (all the LCD code in one file, all the temperature sensor code in another file, that kind of thing).

  • Thank you @Majenko. Will look into code examples more and try some stuff out. Jul 18, 2019 at 15:54
  • When you say header file, you mean C++ .h and .cpp files. I have to define some of the functions in a .h and then do the implementation in the .cpp like normal C++. I don't know why, but when I heard header file, it sounded to me like just one file. youtube.com/watch?v=2pxYEwaMtaI Jul 22, 2019 at 2:39
  • .h and .CPP files are not bound together in pairs. You can have one without the other. .h files are a convenient way of sharing chunks of code between .c and .CPP files. .CPP files get compiled.
    – Majenko
    Jul 22, 2019 at 8:01
  • C++ has no concept of "header file". That is all handled by the C Preprocessor (confusingly called CPP). When you #include a file it is literally included verbatim at that point. C++ doesn't know about it - it only sees the content.
    – Majenko
    Jul 22, 2019 at 9:31
  • Thanks - Majenko. Looked up more stuff on preprocessing and tutorialspoint.com/cprogramming/c_header_files. Jul 22, 2019 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.