I am trying to make a very complicated library, and due to the amount of code it will require, I have separated the code into several different header files, each with its own C++ file. The library (called 'Quadrotor') contains one header and one code file in the root directory. There is a folder inside the root directory that contains the rest of the files. The main sketch includes <Quadrotor.h>, and Quadrotor.h includes all other header files. When I tried to compile it, I received 'undefined reference' errors because the C++ files associated with the additional header files weren't getting compiled. I 'solved' this problem by adding '#include "somecode.cpp"' to the end of each header file. It compiles now, but it fails while linking because it is compiling the code twice. For example, quadrotor.h includes datatypes.h, which contains a vector class. A definition for the vector class occurs in two places, the quadrotor object and the datatypes object.
I have been researching the problem for hours, and have already insured that all of my header files and code files use guards (#ifndef). I have only come here as a last resort.
If it is also possible, I would love to get this thing to compile without requiring the code files to be included at the bottom of each header, but at this point, I'll take anything that will work.
For every .cpp file that you want compiled, you have to include a corresponding .h file in the main sketch.
The main header file is included in the main sketch, and the additional header files are included from the main header file. The problem isn't that the code isn't being compiled, it's that the code is being compiled twice.
pYro_65:
You can prevent alot of multiple definitions by using:
Used in header files
#ifdef HEADER_NAME
#define HEADER_NAME
//Header file contents here.
#endif
Replace HEADER_NAME with what ever you want.
Any libraries your 'library' uses need to be included in the sketch file too.
Thanks for the reply, but I already used header guards. I listed all of the things I have already tried in my original post to help everybody out. Any other ideas?
EricMiddleton:
The library (called 'Quadrotor') contains one header and one code file in the root directory.
What root directory, exactly?
EricMiddleton:
There is a folder inside the root directory that contains the rest of the files. The main sketch includes <Quadrotor.h>, and Quadrotor.h includes all other header files.
Perhaps post your code. This is all vague, describing the problem rather than showing it.
You need to ensure that the extra include files are included within your Quadrotor.h file.
Your extra cpp files need to be in a folder called "utility" within the library's root folder so that the IDE can know where to find them to include them in the compiled code.
majenko:
You need to ensure that the extra include files are included within your Quadrotor.h file.
Your extra cpp files need to be in a folder called "utility" within the library's root folder so that the IDE can know where to find them to include them in the compiled code.
Thank you very much, it works! The IDE finds the code files and everything compiles correctly. A big thank you to everyone else for all the help too!