To add compiler options, you'll need to to either modify Sublime's standard C/C++ build system, or create a new one. The easiest method is to create a new one, but I'll also show you how to edit the existing file.
To create a new build system, open a new file in Sublime with JSON syntax and add the following:
{
"cmd": ["gcc", "--std=gnu99", "-pthread", "-Wall", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"shell": true,
"variants":
[
{
"name": "Run",
"cmd": ["${file_path}/${file_base_name}"],
"shell": true
}
]
}
Save it as ~/.config/sublime-text-3/Packages/User/C_pthread.sublime-build
. To compile with it, open your source file, then select Tools -> Build System -> C_pthread
, then hit CtrlB to build. CtrlShiftB will execute the Run
option.
Editing the Sublime's built-in C/C++ build system is slightly more involved. In Sublime Text 3, packages are wrapped up in .sublime-package
zip archives. To access the C/C++ build file, first install Package Control (if you haven't already), then install the PackageResourceViewer
plugin. Open the Command Palette with CtrlShiftP, type prv
to bring up the PackageResourceViewer
options, select Open Resource
, then navigate down to C++
and select the C++.sublime-build
option. Replace its contents with those above, then save the file. The advantage of this method is that you can select Tools -> Build System -> Automatic
, and whenever you're editing a C file you can hit CtrlB to build and not worry if the correct build system is selected.
One final bit of advice: editing build systems is fine for small, one-off programs, but for larger projects I'd strongly suggest using make
. You can set all your options in your Makefile
, open it in Sublime, hit CtrlB (assuming your Build System is set to Automatic
), and your program will build. This way you don't need to mess around with setting all the command-line options, library includes, etc. in the .sublime-build
file, then change them around again for your next project.
Good luck!