error: multiple definition of `func1()'

Please disregard this first post and scroll down to Reply #5.

How to resolve this error?

In file included from directories.ino:2:
/file1.cpp: In function 'void func1()':
file1.cpp:4: error: 'Serial' was not declared in this scope

#include "file1.cpp"

void setup()
{
	Serial.println("in setup");
}

void loop()
{
	Serial.println("in loop");
	func1();
	while(true) { } 
}
#include <Arduino.h>
#include <Serial.h>

void func1() // error: 'Serial' was not declared in this scope
{
	Serial.println("in func1");
}

I am using Aruduinio IDE 1.0.5r-2

Thank you.

You need to correct your title. The error is actually 'Serial' was not declared in this scope

Try adding #include <Serial.h> ????

...R

Where is Serial**.begin(115200)**?

Thanks Robin2 and HazardsMind.

I added "#include <Serial.h>" to both files.

I added "Serial.begin(115200)" to setup() (I should have caught that :blush:).

But the same error persists :~.

What board are you using? Isn't it SerialUSB or something on one of them?

Belay that, you only get the error in file1 I gather.


Rob

Now I get a different error:

file1.cpp.o: In function func1()': C:\Users\wolf\AppData\Local\Temp\build6950440914927975083.tmp/file1.cpp:4: multiple definition of func1()'
directories.cpp.o:/file1.cpp:6: first defined here
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

directories.ino:

#include <Serial.h>
#include "file1.cpp"

void setup()
{
	Serial.begin(115200);
	Serial.println("in setup");
}

void loop()
{
	Serial.println("in loop");
	func1();
	while(true) { } 
}

file1.cpp:

#include <Arduino.h>
#include <Serial.h>

void func1()
{
	Serial.println("in func1");
}

Apprenetly Arduino can include .h files, but not .cpp files.

This works:

#include "file1.h"

void setup()
{
	Serial.begin(115200);
	Serial.println("in setup");
}

void loop()
{
	Serial.println("in loop");
	func1();
	while(true) { } 
}

file1.h:

#include <Arduino.h>

void func1()
{
	Serial.println("in func1");
}

ok, live and learn eh?


Rob

wolfv:
Apprenetly Arduino can include .h files, but not .cpp files.

I'm not at all a C/C++ expert but my understanding is that .cpp files are never included.

This post has a lot of useful info.

...R

Any file can be included via #include directive. To include cpp is the same as it will be copied whole source exactly at the place where directive is. The problem is, if it is included cpp and arduino IDE adds the same file into project, then first will be added via project and second will be added via directive. Try to use different extension which is not recognized by IDE e.g. .cxx and if you use #include it will be OK.

Place the include inside a namespace, and call that version.

namespace Foo{
  #include "test.cpp"
};

void setup() {
  Serial.begin( 9600 );
  Foo::func();
}

void loop() {}
#include <Arduino.h>
void func(){ Serial.print( "HI" ); }

Or use ifdef guards to prevent showing the definitions when not included.

Apprenetly Arduino can include .h files, but not .cpp files.

Wrong. What is happening is that the contents of the file1.cpp file are being included in the cpp file created from the ino file, which is compiled, resulting in one instance of func1. Then the file1.cpp file is compiled, resulting in another instance of func1. The linker then can't figure out which instance it should include.

Budvar10:
Any file can be included via #include directive. To include cpp is the same as it will be copied whole source exactly at the place where directive is. The problem is, if it is included cpp and arduino IDE adds the same file into project, then first will be added via project and second will be added via directive. Try to use different extension which is not recognized by IDE e.g. .cxx and if you use #include it will be OK.

Thanks Budvar10, that's a good explanation.