Skip to content

Instantly share code, notes, and snippets.

@thomasvanta
Last active September 15, 2016 16:46
Show Gist options
  • Save thomasvanta/9ad802a49742352c1af3c7e0bcabeac0 to your computer and use it in GitHub Desktop.
Save thomasvanta/9ad802a49742352c1af3c7e0bcabeac0 to your computer and use it in GitHub Desktop.

Using Sublime for C++ and openFrameworks

//originally from https://github.com/jmwohl/sfpc/blob/master/C%2B%2B/Using%20Sublime%20for%20C%2B%2B%20and%20openFrameworks.md

Setting up SublimeText for C/C++ development

Two things I knew were going to be important to me: smart code hinting and completion, and auto formatting. Thankfully other folks had already solved these problems for me via a couple handy packages:

1) SublimeAStyleFormatter - for auto formatting
2) Easy​Clang​Complete - for code completion
3) SublimeLinter and SublimeLinter-contrib-clang - for error checks

I installed them via the Package Control. (https://packagecontrol.io/installation)

Setting up SublimeAStyleFormatter Package

I only made one small change here. By default, the formatter plugin uses "ansi" style formatting for brackets — that is, brackets are placed on the line below where the block starts. I prefer to have the bracket on the same line, so I changed the "style" setting in Preferences->Package Settings->SublimeAStyleFormatter->Settings User to "java":

{
    "options_default": {
        // Default bracket style
        "style": "java"
    }
}

There are many additional options which I may investigate as I start to use the formatter more.

Setting up ClangAutoComplete Package

In order to get code completion working, I had to add include paths for my version of Xcode and openFrameworks. After installing SublimeClang, go to Preferences->Package Settings->SublimeClang->Settings User and add the correct paths. For me, it looked like this:

{
    "include_dirs" : [
						"/usr/include",
						"$project_base_path/src",
						"/workspace/$project_name/src",
						"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include",
        					"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1",
						 "/Users/fanta/Code/openFrameworks/of_v0.9.3_osx/libs/**",
						 "/Users/fanta/Code/openFrameworks/of_v0.9.3_osx/addons/**",
						],
}

That "-I" at the beginning is the clang flag for 'include path'.

The Good Stuff - openFrameworks Development

Now here's the exciting part: It's entirely EASY to build OF projects without Xcode! Who knew? Turns out that projects created with the Project Generator include a Makefile that is already set up to work with make. In order to get things working nicely with SublimeText, I created a new build system called OF. To create a new build system go to Tools->Build System->New Build System..., and add the following:

{
    "cmd": ["make Debug && make run Debug"],
    "working_dir": "${project_path:${folder}}",
    "shell": true,

    "variants": [
        { 
            "cmd": ["make && make run"],
            "name": "Run",
            "shell": true
        },
        { 
            "cmd": ["make clean"],
            "name": "Clean",
            "shell": true
        }
    ]
}

Additional Notes

You should be able to build now by hitting cmd-b. If the application that gets built ends up with a weird name, it might be because the path to the src has spaces in it - this was the case for me. You can explicitly set a name in config.make using

APPNAME = SomeName

Extra: Sync Settings with dropbox

https://packagecontrol.io/docs/syncing

Overview of the Process So Far

Ok. Now we should be able to work on new OF projects by following these steps:

1) Create a new OF project with the projectGenerator
2) In the terminal, navigate to the generated project dir, and type 'subl .'' to open it in sublime.
3) Once the project opens, choose 'Project->Save Project As...'' from the menu and give it the same name as you did in the project generator.
4) Start writing code!  When you're ready to build, hit 'cmd-b'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment