Making my own project global functions

Hello,

I have some functions that id like to use in many projects since they are ease some tasks tremedously. So far right now ive been using a HelpFunc.h header in the include directory which worked great. Problems occured when I tried to use these functions in more places like a .h and/or .cpp file for a private libary. Even though I have include guards in my header it creates an error with redefining functions of the header content. I googled some stuff about it and apparently I shouldnt straight define the functions in the header. So i just declared them there and made a .cpp file for the definitions. This hasnt helped as well.

Ive already made lots of libaries and just headers for my projects but never encountered this persistent error. Could you please enlighten me to how exactly and in which order does the compiler take its .h and .cpp files and on wether there is a better solution to my case.

In short I basically wanna have some arduino like functions (digitalWrite) which I can use anywhere. In main.cpp, in include.h and in bar.h or bar.cpp.

Thanks in advance, for your time!

The .cpp for definition and .h for declaration is the correct format.

My exact problem persisted because I was using template functions. These template functions managed to compile without a problem only when being in the header file.

So as an example:

HelpFunc.h

void normalFunc();  //only declare

template <typename T>
void templateFunc() {
   //do stuff in header right away, if this were to be in .cpp I wouldnt get a succesfull compilation
}

-------------------------------------

HelpFunc.cpp

void normalFunc() {
  //do stuff in .cpp
}

-------------------------------------

Overall this way of programming it solved my issue. I am not sure wether this is the intended behaviour.

If anyone knows Id still appreciate an explanation.

1 Like

Here’s an useful StackOverflow questions:

1 Like

Thanks for especially the second one, there was an exact explanation which helped a lot! :slight_smile: