0

I am trying to build a C++ project in Sublime Text 3 on Linux. Normally I just swap to the terminal and run make run and it runs. However, I'm trying to streamline the process by running directly in Sublime. However, pressing Ctrl+Shift+B brings up only two options: Make and Make clean. Both of which will do the command according to my Makefile for the project. However, there is no Make run, so I can't directly run the project by pressing Ctrl+B. I hope someone can provide some insight into this!

Here is my Makefile:

BINARY := build/project
SRCS := $(shell find . -iname "*.cpp")
OBJS := $(addprefix build/,$(SRCS:.cpp=.o))

NEEDED_LIBS := 

CXX := g++ -flto
LD := $(CXX) $(addprefix libs/,$(NEEDED_LIBS))
override CXXFLAGS += -std=c++11
override LDFLAGS += $(CXXFLAGS)

all:$(BINARY)

$(BINARY):$(OBJS) $(addprefix libs/,$(NEEDED_LIBS))
    $(LD) $(LDFLAGS) -o $@ $^

build/%.o:%.cpp
    $(CXX) -O2 -c $(CXXFLAGS) $< -o $@

run:$(BINARY)
    @$(BINARY)
    
clean:
    rm -rf build/*
MattDMo
  • 100,794
  • 21
  • 241
  • 231

1 Answers1

2

The default build for this is Makefile/Make.sublime-build, which looks like this:

{
    "shell_cmd": "make",
    "file_regex": "^(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${folder:${project_path:${file_path}}}",
    "selector": "source.makefile",
    "syntax": "Packages/Makefile/Make Output.sublime-syntax",
    "keyfiles": ["Makefile", "makefile"],

    "variants":
    [
        {
            "name": "Clean",
            "shell_cmd": "make clean"
        }
    ]
}

That is, by default it just runs Make, but it also has a variant named Clean that will run make clean as well. In particular, this points out that the list of commands is static and doesn't rely on the actual content of the Makefile in question.

What you can do is choose Tools > Build System > New Build System from the menu, which will create a new tab and provide a default build system. Select all of that content, and then replace it with a copy of the above so you have a duplicate of the base build system. Then, you can add in additional variants as needed. For example:

    "variants":
    [
        {
            "name": "Clean",
            "shell_cmd": "make clean"
        },
        {
            "name": "Run",
            "shell_cmd": "make run"
        }
    ]

Once you're done, save the file in the location Sublime defaults to (your User package) with a custom name, like Custom Makefile.sublime-build or such (the location and the extension are the important parts).

Once you've done that, you will see your build system appear in the menu under Tools > Build System with the name that you saved the file as. You can either select the build there to use it, or leave it set as automatic.

When you use ctrl+shift+b next, the options from this build system will appear in the list (if the build is set to automatic, you'll see the default options as well).

OdatNurd
  • 21,371
  • 3
  • 50
  • 68