Sublime Text C++ Compiler Mac

The below was written for clangd, but much applies to cquery and ccls as well.

CCLS#

Setting up C for competitive programming in Sublime Text. I use and recommend Sublime Text over other editors/IDEs because it is light weight and fast. IDEs in general are heavy and slow. Sublime also has many shortcuts and features to explore. MinGW (GNU C/C compiler collection for Windows 64-bit) Download MinGW. You create a script in linux to compile and execute the source code in the Terminal and you store it in your Documents folder named as runC.sh. #!/bin/bash gnome-terminal -e '/bin/bash -c 'gcc -Wall $1 -o $2;./$2; echo; read -p 'Pressentertoscape.' ; exit; exec /bin/bash'; &'. Through a Sublime Text Build System, you call this script.

Build and install from source or download for your distribution.See the ccls wiki for more details.

Clangd#

Install Sublime Text 3

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:

Sublime Text C++ Compiler MacAnd 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.

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.

C Compiler For Mac

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#

Sublime Text C++ Compiler Macro

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.