For some, Sublime Text sets the bar when it comes to text editors. It’s a beautiful feature-rich text editor for editing code that puts a premium on user experience. Its features include a. For the Mac people, it meant that they didn’t know how what tool to use apart from X-Code which of course didn’t work (but it did for C) and me, it meant that I had to find a way to compile my code written in mere text editors like Atom or Sublime. Just follow below steps to configure sublime text to compile and run C and C programs. I have tested the steps in sublime text 3 but I am sure it will work for any other version also. Also Read: Configure Notepad to Run C, C and Java Programs. How to Run C and C Program in Sublime Text. Here is a slightly more advanced example that has exclusions to reduce clutter. This one was made for Chrome on a Windows machine and has some Visual Studio specific excludes. Save this file in the same directory as your.gclient file and use the.sublime-project extension (e.g. Chrome.sublime-project) and then open it up in Sublime. C11 features usage. Here you will need a custom build system for Sublime text. It's easy to create! 3 easy steps again. 1) Download gpp.sublime-build from my GitHub. 2) Go to Sublime, then select Tools-Build System-New Build System. This is the place where you should paste gpp.sublime-build. Don't forget to save the file.
There are many things that I find missing in default C++ build in Mac Sublime Text(clang) like no bits/stdc++.h header, regular warnings on using auto keyword, using inline comparator functions etc. So I tried searching how to use brew's g++ as default build in Sublime Text without breaking anything but couldn't find anything.
First of all install brew from brew.sh.
After that install gcc using the command in terminal 'brew install gcc'.
Check if install is complete using command 'gcc-11 --version' (I got gcc-11 installed, in future you may use gcc-12,gcc-13 etc).
Install Sublime Text 3
Now type the command 'which g++-11' to get the location where g++-11 got installed. Copy this location ( Mine was something like — /opt/homebrew/bin/g++-11 ).
Open sublime text and install 'PackageResourceViewer' from Package Control. After that open command palette and open 'PackageResourceViewer: Open resource'.
Then go to C++.
Then go to C++ Single file.
Finally replace all g++ with the location we copied earlier (/opt/homebrew/bin/g++-11).
Save this file using cmd+s and restart sublime text. Compile and build as you would usually do in sublime text (cmd+b/cmd+shift+b) and this time it would build using brew's g++.
If you would like to revert back to original g++, just go back to C++ Single file build and change '/opt/homebrew/bin/g++-11' to 'g++' wherever applicable.
Thanks
C/C++ language servers
The below was written for clangd, but much applies to cquery and ccls as well.
CCLS
A newer project emerged from cquery.Build and install from source, see ccls wiki
Cquery
Build and install from source, see cquery wikiNote that work on cquery has stopped. Prefer using ccls or clangd.
Clangd
Sublime Text C++ Compiler Machine
To use clangd on Debian/Ubuntu, add the apt repositories described here.After that, install with e.g. apt install clang-tools-9
. The clangd executablewill have a version number suffix. For instance, clangd-9. You will thus have toadjust your 'clients'
dictionary in your user preferences.
To use clangd on Mac, use Homebrew: brew install llvm
. The clangd executablewill be present in /usr/local/Cellar/llvm/version/binYou probably need to install the Xcode developer command-line tools. Run the following in a terminal:
And if you're on macOS 10.14, also run the following to install essential headers like wchar_t.h
:
To use clangd on Windows, install LLVM with the LLVM installer,and then add C:Program FilesLLVMbin to your %PATH%.
Compilation database
For any project of non-trivial size, you probably have a build system in placeto compile your source files. The compilation command passed to your compilermight include things like:
- Include directories,
- Define directives,
- Compiler-specific flags.
compile_commands.json
Like any language server, clangd works on a per-file (or per-buffer) basis. Butunlike most other language servers, it must also be aware of the exact compileflags that you pass to your compiler. For this reason, people have come up withthe idea of a compilation database.At this time, this is just a simple JSON file that describes for eachtranslation unit (i.e. a .cpp
, .c
, .m
or .mm
file) the exactcompilation flags that you pass to your compiler.
It's pretty much standardized that this file should be calledcompile_commands.json
. clangd searches for this file up in parentdirectories from the currently active document. If you don't have such a filepresent, most likely clangd will spit out nonsense errors and diagnostics aboutyour code.
As it turns out, CMake can generate this file for you if you pass it thecache variable -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
when invoking CMake. It willbe present in your build directory, and you can copy that file to the root ofyour project. Make sure to ignore this file in your version control system.
Sublime Text C++ Compiler
If you are using a make-based build system, you could use compiledbto generate a compile_commands.json
.
Since header files are (usually) not passed to a compiler, they don't havecompile commands. So even with a compilation database in place, clangd willstill spit out nonsense in header files. You can try to remedy this byenhancing your compilation database with your header files using this project called compdb.
To generate headers with compdb, read this closed issue.
You can also read about attempts to address this on the CMake issue tracker, along with the problemof treating header files as translation units.
compile_flags.txt
Another way to let your language server know what the include dirs are is by hand-writing a compile_flags.txt file inyour source root. Each line is one flag. This can be useful for projects that e.g. only have a Visual Studio solutionfile. For more information, see these instructions. Creating this file by hand is a reasonable place to start if your project is quitesimple.