[SOLVED] Separate `ofApp.cpp` in multiple files the same way processing IDE do it?

How can I separate a program in multiple files the save way you can do it with the Processing IDE. I have read this post:

http://forum.openframeworks.cc/t/is-is-possible-to-separate-long-testapp-cpp-files-into-many/12466

but example files are not accessible.

What I’m trying to do is separating ofApp.cpp in multiple files.
It is probably simple but haven’t find a solution yet.

Hi @lapinchatware,

I don’t know Processing, so I’m just guessing: are you trying to separate your ofApp methods’ definition into multiple files ?

If that’s the case, you can always create a new .cpp file in your src folder and cut/paste some methods in there. As long as you #include your ofApp.h, and keep all your declarations inside that .h, it’s all good. Try to group your methods by functionality if possible, and name your .cpp’s with an explicit name.

Example:

MyClass.h

class MyClass {
    void method1();
    void method2();
    void util1();
    void util2();
}

MyClass.cpp

#include "MyClass.h"
void MyClass::method1(){}
void MyClass::method1(){}

MyClass_Utils.cpp

#include "MyClass.h"
void MyClass::util1(){}
void MyClass::util2(){}

Now, if you want to actually improve your coding style, try to properly re-factor the whole thing, keeping OOP in mind.
Your objective should be to have no redundancy in your code, to properly encapsulate, etc.
It’s always nice when inside your ofApp you have very high level instructions like:

MyObjects.complexStep1();
MyObjects.longAndDifficultStep2();

With all the complexity hidden inside those objects.

Cheers mate !

Thanks you @sachaventura,

It is exactly what I want to achieve! Yes exactly! Your example is perfect and working as expected.

For Processing users: it is the same as adding a tab in Processing IDE (which add a file in your sketch folder). Don’t forget to add your new files to your openFrameworks project. In Code::Block it’s as simple as right clicking the project name, clicking add Files and choose the newly created files to include in your program.

@sachaventura In this case I only want to separate my code in meaningful pieces. I’m already aware of OOP(I’m good at it but not a professionnal yet) and using the concept when I need it.

Thanks you again! :smiley: