I just can't wrap my head around on how to properly work with multiple files. My code has been getting quite big so I decided to split all the classes into one file each. So I'm left with 6 files:


header.cpp includes all the header files, and main.cpp the setup() and loop() functions.

Visual Studio Code shows me no errors while compiling. The includeguards have the name convention: CLASSNAME_INCG

So in my main.cpp for example I have something like:

#include "header.cpp"

    #include "SensorData.cpp"

    #include "DataHandler.cpp"



When going into a class-file I have it defined in the following way (fileHandler.cpp):

#include "header.cpp"


class FileHandler{


So in summary I include the headerfile into every file, with includeguards arround. If I wouldn't include it I would get errors that crucial libaries like Serial etc. are missing. But when including the header into every file compiliing works just fine, but when it comes to linking the files togehter I get an error of multiple definitions.

So how can I define the headerfile just once for all files albeit I need it in every file?

  • you use the guards wrong. see some library for correct use and use .h files
    – Juraj
    Commented Jan 5, 2019 at 9:29

1 Answer 1

  1. Rule #1:
    Header file extension is ".h". Files with ".cpp" are ususally compiled as separate compilation units into the object file. If you have any function definition (= declaration + implementation) or any global variable in more object files, it'll fail to link.

  2. Rule #2:
    No definitions in headers - variables and functions will cause this error if you include .h file into more .cpp files. If you need super global variable, you have to use declaration with extern modifier and you have to define it in one of the .cpp files. But only one! Also there can be only function prototypes (declarations) in header file. Implementation have to be only once in one of the ".cpp" files.

It's usually in the first chapter of books about learning C/C++.

Really short example:

// myLib.h
#ifndef _myLib_h_guard_
#define _myLib_h_guard_

void myFunction(); // function prototype (declaration)

extern int mySuperGlobalVariable;  // variable declared as extern, there must be definition somewhere

class MyClass {
  int someMethod(); // method declaration

  int anotherMethod() { return 1; }  // definition inside of class works, but sometimes is less readable

// myLib.cpp
#include "myLib.h"

void myFunction() { // function implementation (definition)
  // do something...

int mySuperGlobalVariable; // definition

int MyClass::someMethod() { return 2; } // method definition
// main.cpp
#include <myLib.h>

int main() {

  mySuperGlobalVariable = 10;


  MyClass instance;
  int first = instance.someMethod();
  int second = instance.anotherMethod();
  • Could you maybe give me an example on how to do this properly? I'm sure this sounds trivial to you but I hardly understand what you've said. Commented Jan 5, 2019 at 10:22
  • Thanks a lot :) I tried to declare and initialize it in myLib.cpp and forgot the extern-keyword in myLib.h. Commented Jan 5, 2019 at 10:47
  • 1
    you can use #pragma once instead of the old fashioned #ifndef _myLib_h_guard_ :p Commented Jan 5, 2019 at 10:54

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.