OF in Eclipse on Linux

Saturday, a few of us were trying to get OF working with Eclipse. I had it working sorta cheaply–it required popping out to the shell to run the application once it compiled. Since then, I’ve done some more work and have a better solution.

Eclipse/CDT has it’s own way of setting up C++ projects, but it also allows you to import existing Makefile projects. This is essentially what I did. I started with the Makefile version of OF 0.04 and hooked it up to Eclipse.

Now I am able to compile, run, and debug from Eclipse, not to mention leverage the nice code-completion and syntax highlighting features. I am running Ubuntu Gutsy, Eclipse 3.3, and GCC. Here are the steps:

[list type=decimal]

  • Install CDT via Europa Discovery Site in Eclipse

  • Make sure you have gcc installed

  • Download the Makefile version of OpenFrameworks and get it running from the command line.

  • In Eclipse, create a new workspace for your OpenFrameworks stuff

  • Add OpenFrameworks as a generic project in the workspace.

[li]There are a couple of ways to do this. The easiest way is to create an empty project and then copy the files from openframeworks into it.
[/li]

  • Create a new example project
    [list type=decimal]
    [li]Create an empty C++ Makefile project w/ the same name as the example you want to use

[li]This type of project will automatically use your build file to build the application
[/li]

  • Copy the contents of the of an example app into that project, ie
  
cp -r ofWorkspace/of/apps/graphicsExample/* ofWorkspace/graphicsExample  

[/list][/li]

  • Right click, refresh the example project to see the new files in your project explorer

  • Modify the Makefile so that the paths point to the correct place in the Eclipse workspace
    [list]
    [li]I created a constant in the makefile and included it everywhere in the path, for example:

[li]Near the top:

  
MYOF = /of  

  • Then later:
  
LDFLAGS = -L$(PWD)/..$(MYOF)/libs/fmodex/lib  

(replacing …/…/ with …$(MYOF)/ everywhere in the file)
[/li][/list][/li]

  • Build the project - if you set up your Makefile correctly, it should build fine, putting the executable in the project’s bin directory, etc.

  • Next, set up the example project to run from Eclipse
    [list type=decimal]
    [li]In the project properties window, select “Run/Debug Settings” then “New…” then “C/C++ Local Application” then “OK”

  • In the “Edit launch configuration properties” dialog, give the configuration a name

  • In the “Main” tab, C/C++ Application field, enter relative path to the binary, i.e.: bin/graphicsExample

  • Under the Environment tab, create a new variable called LD_LIBRARY_PATH and set the value to bin/libs/

[li]This does the same thing as the clickToLaunchApp.sh script which is created by make.
[/li]

  • Click OK to save this launch configuration and close the properties window
    [/list][/li]

  • Click the green play button to run the example. It should work
    [/list]

Debugging:
[list type=decimal]

  • update your makefile w/ the -g flag for the files you want to debug

[li]i.e.:

  
$(CPP) -g -c src/testApp.cpp -o obj/testApp.o $(CXXFLAGS)  

[/li]

  • make clean to remove previous compiled binaries

[li]in eclipse, this is under ‘Project’ -> ‘Clean’
[/li]

  • click the little bug button and select the run configuration for the current project

[li]This should switch you to the debug perspective and bring you to any breakpoints you set in Eclipse.
[/li][/list]

Data files, etc

You either need to copy the data directory from bin into the project root directory or symlink it to get examples like moviePlayerExample to work.

More

I hope this helps anyone who prefers to use Eclipse on Linux as their IDE. I’m sure if I spent some more time, I could make OF work like a regular Eclipse/CDT project instead of a Makefile project, but this really satisfies all my needs. If this is confusing, let me know. Once I make sure I know what I’m doing and have this working for a while, I may be able to make a better tutorial (with graphics and real HTML!).

Regards,
Brian

As I started using my solution above, I was disappointed that the code completion only worked for classes, etc. that I defined locally and not for things in the rest of the framework.

Then I saw what Matt was up to, attempting to get OF working in Eclipse on a Mac ( http://forum.openframeworks.cc/t/of-in-eclipse-on-a-mac/447/0 ), and it inspired me to seek out a more integrated Eclipse solution.

Some initial notes:

  • First of all, I am continuing to keep OF as a separate project in my workspace and create new OF projects next to OF that reference it. I know this sorta breaks the general structure of OF (having the app directory within OF), but it made more sense to me in terms of how Eclipse wants to do things

  • The second thing I want to mention is I based this work of the Linux Makefile project. I understand how the Makefile works and I figured that if I could make Eclipse work like the Makefile, then I would be in good shape.

  • I was clued in to a couple of things by Matt’s posts. The first was to start with the Hello World C++ project in CDT. The second was location of things like like the “Other objects” in the Linker settings area. Thanks Matt.

Here’s a (relatively brief) rundown of what to do
[list type=decimal]

  • Add OF as a project (I called it “of”) in your workspace.

  • Start with a Hello World App. Build and Run it in the Debug configuration. For now, work on the Debug configuration (later copy the settings over to the Release configuration and turn on the optimization flags and turn off the debug flags).

  • Create source directories: src should already be there. Copy or symlink openframeworks from the of project into your new project. Under Project Properties->C/C++ General->Paths and Symbols->Source Location add the openframeworks cpp file directories (ie openframeworks/utils/). I had to rename QTUtils.cpp so it wouldn’t pick it up. This line is in the Makefile and the cpp file is there, but the makefile never builds it and it gives errors if you try to. I think it was left there accidentally.

  • Add the include paths: Project Properties->C/C++ General->Paths and Symbols->Includes->GNU C++ Add all the stuff listed in the makefile as INCS and add them as workspace folders that reference the of project. This is a pain in the ass, as Matt mentions in his post. Here’s an example of what one entry looks like: /of/libs/rtAudio. There will be something like 18 entries in this list when you are done.

  • Apply these changes and check Project Properties->C/C++ Build->Settings->GCC C++ Compiler->Directories->Include Paths (-I). This should be populated with the includes you just added.

  • While we’re here, go down to GCC C++ Linker->, under libraries (-l) add glut, GLU, asound, and fmodex. Under -L add “${workspace_loc:/of/libs/fmodex/lib}” or browse to the workspace dir in your of project where fmodex/lib is.

  • Still in the linker area under Miscellaneous “Other objects” add all of the libraries listed as LIBS in the makefile from your OF workspace project. each one should look something like: “${workspace_loc:/of/libs/rtAudio/RtAudio.o}”

  • Still in the settings area, but under a different tab, Build Steps, add a this line as a Post Build Step: “cp -r $(PWD)/…/…/of/export/libs .” You should see in the Makefile where this comes from.

  • After doing all of this, take a break 'cause you’re probably going crazy. Then cross your fingers and try to build the thing. Your output will look a bit more verbose than original Makefile output, but it should compile and link.

  • Next set up your Run Configuration. Project Properties->Run/Debug Settings.

  • Select the current run configuration and edit it. The executable should already be there and is probably something like “Debug/mybinary”. Under the environment variables tab, set LD_LIBRARY_PATH to Debug/libs. When you want to create you Release configuration, you have to create a new run configuration.

  • Now you can try to run your project. The easiest thing to do is to copy the source files from an example (TestApp.h, TestApp.cpp, main.cpp) into your src dir. Clean, Build, Run.
    [/list]

This took me forever to figure out and it wasn’t very fun. However, code completion works really well now and so does debugging and the rest. When I want to create a new project, I just right click my old project, copy it and then paste it into the workspace with a different name. You still have to adjust your Run configuration (LD_LIBRARY_PATH disappears in the copied version).

I hope this helps someone. I can hook anyone up w/ any file they think they’ll benefit from, so if you want my .cproject file or even my entire workspace (it’s over 150MB!), let me know. If this doesn’t really help anyone, well at least it was a good learning experience.

-Brian

thanks for the tutorial

Yeah this rocks!

Thanks for the feedback. I’m glad these instructions help.

Also, recently, I was trying to get it more like the other openframeworks IDE packages, where OF and the example apps are all in the same project. My co-worker showed me a trick that makes things WAY easier to implement and understand.

  • Start w/ an empty makefile project

  • Drop the contents of the openframeworks makefile project into that

  • Open the Build Targets view and browse to an example you want to try

  • Add a new target for “all” in the example directory

  • Add a new target for “clean” in the example directory

  • Modify the Makefile slightly by removing $(PWD)/ everywhere you see it

  • Double click on the “all” target and it should compile and put the right stuff in the example’s bin directory (double clicking on clean cleans out bin)

  • Under Project->Properties add a build configuration for the new executable you just created

  • Under build configuration, set LD_LIBRARY_PATH

  • That’s it

This trick just uses the existing makefile (like I did in the first example) without screwing up the existing openframeworks file structure. Apparently Eclipse will automatically parse your makefile to find your libraries for code completion! (I think this is a side effect of creating the build target) If you want to debug, add -g to the file targets you’d like to debug.

Right now I have it working this way w/ 0.04, and I was working on 0.05 based on some stuff Zach gave me on Saturday at the workshop. I’ll probably hold off on that until the Linux makefile version of 0.05 is ready and then set up a base project using this method and make it available to whoever wants it. If you want my 0.04 project, or if you have questions on setting it up this way, let me know.

Lastly, sorry for all the posts w/ conflicting info. I’m just learning about CDT and OF, and posting what I discover as I go.

-BB

Cool! I like that this uses the makefile directly as I’ve been trying to learn about make files as well as C++ using OF.
I only had one problem with the above solution. The examples that use external files don’t seem to be able to find the data folder.

The examples that use external files don’t seem to be able to find the data folder.

do they work when you run them (via shell script) from the terminal or desktop / explorer?

I’m not too familiar with eclipse, but some compilers let you choose the directory to run the app (or in this case, shell script) from. That directory is current working directory of the app, and that can mess up file loading (ie, it looks for files in the folder called data, and if the cwd is different, I might not find that folder even if it is next to the app).

hope that helps - we’re very happy to see the eclipse stuff coming together, alot of folks are excited about this and the more compilers / places to work w / OF, the better.

take care!
zach

Thanks Zach. That pointed me in the right direction. I poked around and discovered that you can change the working directory under the Project->Properties->Run/Debug Settings->Arguments tab
Now I can run the app from Eclipse and it loads external files correctly.