How to create a library for a function and a struct?

I found some examples of putting a class into a library, but I only have two functions and two structs and putting them in a class wouldn’t make much sense. Nevertheless I would like to move them to another file that I can include in different projects.

How would I do that?

struct ExampleStruct1{
  float foo;
  int bar;
};

struct ExampleStruct2{
  float abc;
  char test;
};

ExampleStruct1 ExampleFunction1(int foo) {
    // ...
}

ExampleStruct2 ExampleFunction2(ExampleStruct1 bla) {
    // ...
}

Should I just paste all that into a .h or .hpp file and be done with it?

In the folder where your sketches are stored there should be a folder called libraries. If not, make it. Inside that folder, make another folder for your library. I call my folder utilslib, but you can call it whatever you want.

Inside that folder, put the following .h and .cpp files: .h header

struct ExampleStruct1{
  float foo;
  int bar;
};

struct ExampleStruct2{
  float abc;
  char test;
};

ExampleStruct1 ExampleFunction1(int foo);
ExampleStruct2 ExampleFunction2(ExampleStruct1 bla);

.cpp code

#include "___.h" // above file

ExampleStruct1 ExampleFunction1(int foo) {
    // ...
}

ExampleStruct2 ExampleFunction2(ExampleStruct1 bla) {
    // ...
}

In C++ it is important to separate the declaration of the function from it's definition. That's why you need to split functions up into an .h and .cpp file.

Thanks a lot. :)

Edit: Just in case I ever decide to make a class out of it; where would I put the structs then?

felic: Thanks a lot. :)

Edit: Just in case I ever decide to make a class out of it; where would I put the structs then?

This really is determined by your needs. You'll need to ask yourself: do they make up the class functionality? Or are they simply used by the class (just like an 'int' variable), and can have meaning in code that does not use the class.

Basically you can leave them where they are, or you can put them in the class if you do not want them to be global. Your code can be your own creation. there are many "do's" and "do not's", but they are really only guidelines.