Compiling a multi-file Arduino project

I’m using Arduino 023 on Windows XP, SP3.

I’ve created a very basic sketch that declares one variable.
The skecth name is TwoFiles.pde.

The sketch sets that variable to 0 in setup(), then sets that variable to 1 in loop().
It compiles & runs just fine.

Now- I’ve added a second file using the New Tab menu option, and named the file “file2.cpp”.
file2.cpp resides in the same directory as the main file.

file2.cpp contains 1 function- printFromFile2() which “attempts” to print a variable that was declared in
TwoFiles.pde.

file2.cpp cannot find the Serial.print() function. "error: ‘Serial’ was not declared in this scope"
Ideally, I would also like to use header files which are included in two or more source files.

Would someone point me to a simple, multiple-file project- that compiles and runs?

I’m attaching both files for reference.

Thanks

TwoFiles.pde (159 Bytes)

file2.cpp (96 Bytes)

What happens if you rename file2.cpp to file2.pde?

It works fine.

Is a previous thread, Someone suggested leaving the "main" file with the pde extension, and all others .cpp. I like this method.

I don't like the way the IDE tries to manage files, projects and directories. (And it doesn't seem to be documented well) It's not obvious what it tries to do, until you try do do things that way you're used to, and they don't work.

Thank you!

I don't like the IDE very much either. I use an external editor with separate windows for all files. As for me I use all PDE files in the same folder and use CPP for libraries, but lots of methods work.

A number of things happen as the IDE creates a .cpp file from a .pde file. For one thing, some files get included. If you want to skip the .pde -> .cpp conversion process, you need to perform the actions that the IDE does. It includes, in 0022, WProgram.h in the .cpp file that it created from the .pde file. WProgram.h includes HardwareSerial.h, where the Serial instance is defined.