Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently Using SublimeText with SublimeClang for CMake/C++ Projects

I have been trying to play with SublimeText2 for some time now. While it is very easy to work with Python in it almost out of the box, working with C++ is a bit more tricky. I can manage to set up a CMake build script by copying and modifying the existing Makefile script, but there are many things that just don't work as they do in a CMake supported IDE, like Eclipse CDT. SublimeText 2 does not seem to understand the concept of a separate build directory, it also cannot get me autocomplete through SublimeClang, if I include the libraries with reference to the directories added in CMake. SublimeClang keeps complaining that it cannot find the libraries, and when I try to #include, it cannot even offer me autocomplete on standard STL header file names, e.g., algorithm. If someone has a pipeline figured out, I would be obliged to hear about it.

I have asked this question in more general purpose usage-related forums before, where I did not get any response, which is why I thought of posting it here.

like image 760
Subhamoy S. Avatar asked Sep 05 '12 22:09

Subhamoy S.


1 Answers

I use Sublime Text 2 with CMake and SublimeClang. I also use SublimeGDB. My build directory is under [project root]/build. Have a look at my project file and see if it helps you:

{
    "folders":
    [
        {
            "path": "."
        }
    ],

    "build_systems":
    [
        {
            "name": "Build",
            "cmd": [ "make", "-C", "build" ],
            "file_regex": "/([^/:]+):(\\d+):(\\d+): "
        }
    ],

    "settings":
    {
        "sublimegdb_commandline": "gdb --interpreter=mi myapp",
        "sublimegdb_workingdir": "build",

        "sublimeclang_options" :
        [
            "-Wno-reorder"
        ],
        "sublimeclang_options_script": "${project_path:scripts/compileflags.rb} ${project_path:build}"
    }
}

The compileflags.rbscript is used to search for flags.make files in the CMake build tree which is where CMake keeps its compile flags. These flags are needed so that SublimeClang knows where to find your includes.

Here is that script, located under scripts/:

#!/usr/bin/env ruby

# Searches for a flags.make in a CMake build tree and prints the compile flags.

def search_dir(dir, &block)
    Dir.foreach(dir) do |filename|
        next if (filename == ".") || (filename == "..")
        path ="#{dir}/#{filename}"
        if File.directory?(path)
            search_dir(path, &block)
        else
            search_file(path, &block)
        end
    end
end

def search_file(filename)
    return if File.basename(filename) != "flags.make"

    File.open(filename) do |io|
        io.read.scan(/[a-zA-Z]+_(?:FLAGS|DEFINES)\s*=\s*(.*)$/) do |match|
            yield(match.first.split(/\s+/))
        end
    end
end

root = ARGV.empty? ? Dir.pwd : ARGV[0]
params = to_enum(:search_dir, root).reduce { |a, b| a | b }
puts params
like image 78
Emil Eriksson Avatar answered Oct 05 '22 05:10

Emil Eriksson