Help me out here. I don't know what I'm missing. I have this code in my arduino IDE
#include "Foo2.h"
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
And I have five files: Foo.h, Foo.cpp, Foo2.h, Foo3.cpp and Foo.h
Foo.h
#ifndef Foo_H
#define Foo_H
class Foo
{
public:
Foo();
public:
bool Fooa();
bool Foob();
};
#endif
Foo.cpp
#include "Foo.h"
Foo::Foo()
{
}
bool Foo::Foob()
{
return true;
}
Foo2.h
#ifndef Foo2_H
#define Foo2_H
#define LIBRARY_VERSION 1.0.0
#include "Foo.h"
bool Foo::Fooa()
{
return true;
}
#endif
Foo3.h
#ifndef Foo3_H
#define Foo3_H
#include "Foo2.h"
class Foo3
{
public:
Foo3();
Foo _foo;
};
#endif
Foo3.cpp
#include "Foo3.h"
Foo3::Foo3()
{
}
And it is give me an error:
libraries\Foo\Foo3.cpp.o (symbol from plugin): In function `Foo::Fooa()':
(.text+0x0): multiple definition of `Foo::Fooa()'
sketch\kk2.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
what i'm doing wrong
regards,
-
Please show us "Foo3.cpp" that is mentioned in the error.the busybee– the busybee2020年11月27日 07:10:15 +00:00Commented Nov 27, 2020 at 7:10
1 Answer 1
If you want Fooa()
to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:
#ifndef Foo_H
#define Foo_H
class Foo
{
public:
Foo();
public:
inline bool Fooa();
bool Foob();
};
#endif
Alternately, you can provide the function definition directly inside class definition; this also makes the function inline
despite not requiring the inline
keyword.
Or if you want to keep Fooa
as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob()
.
Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site; it informs the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of
error when a function definition is inserted into multiple .cpp file by way of an #include
.
As a side note: #include
is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include
uses transitively) into place where the #include
directive appeared. It does not behave the same way that many similar looking directives in other languages do. E.g. #include
does not behave like the import
keyword does in say Java or python for example.
-
It didn't work adding the 'inline' keyword. I have edit part of my question, because I have other files too.PartnerTech– PartnerTech2020年11月27日 22:16:28 +00:00Commented Nov 27, 2020 at 22:16
-
I'm trying to solve this [arduino.stackexchange.com/questions/79641/…. And I think that I have to figure out this problem first.PartnerTech– PartnerTech2020年11月27日 22:18:13 +00:00Commented Nov 27, 2020 at 22:18
-
No, you were right it works. I have to put the 'inline' keyword in the Foo.h and Foo2.cppPartnerTech– PartnerTech2020年11月27日 22:21:18 +00:00Commented Nov 27, 2020 at 22:21