Sublime Forum

How do I use a different build system for C and C++?

#1

I have two files open in Sublime. One is a C file and one is a C++ file.

I have also created to unique build systems for C and C++ in Sublime called C and C++ respectively.

When I build the C++ file using the CTRL+SHIFT+B shortcut with the C++ source file tab active, it builds properly using the appropriate build system. However, when I press CTRL+SHIFT+B with the C source file tab active, it still tries to compile it using the C++ build system (g++) as opposed to the C build system (gcc).

Is there a way to make it so that Sublime selects the build system that should be used based on the currently active tab and the file extension?

0 Likes

#2

Generally you would do this by making sure that each build has an appropriate selector that specifies the top level scope for that file type and that the build system is set to Automatic in the build menu.

For example, you’d have this in the build system for C:

"selector": "source.c"

And this for the build system for C++:

"selector": "source.c++",

There are other automatic selection methods (keyfiles for a list of files that need to be present, such as Makefile and file_patterns for specifying what extensions a file is for) but they also rely on the build system being set to Automatic.

If you’re doing this but it doesn’t seem to be working, can you share your build files here? That may shed some light on what’s going on.

1 Like

#3

Yeah so it seems I am already using the selector flag but it doesn’t seem to be making a difference.

Here are my build systems for C and C++ respectively.

C:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
"variants" : 
    [
        { 
            "name": "Run", 
            "cmd" : ["${file_base_name}.exe"]
        }
    ]
}

C++:

{
 "cmd": ["g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "working_dir": "${file_path}",
 "selector": "source.cpp, source.c++",
 "shell": true
}
0 Likes

#4

That seems to work fine for me (except for how I’m on a Linux box and there is no start command).

As a first step, I would double check that the Tools > Build System is set to Automatic and not your C++ build file.

1 Like

#5

I had it set to the C++ build file, not automatic. You have no idea how hard I’m facepalming right now.

Anyhow, thank you for the help.

1 Like