How to compile and run C in Sublime Text
Install the g compiler (Windows uses MinGW-w64, macOS runs xcode-select --install, Linux executes sudo apt install build-essential); 2. Create a C.sublime-build file in Sublime Text and fill in the specified JSON configuration; 3. After opening the .cpp file, press Ctrl B to compile, press Ctrl Shift B to select Run to compile and run, and the output results will be displayed in the bottom panel.
To compile and run C code directly in Sublime Text, you need to set up a Build System . Here's how to do it — it's simple and works on Windows, macOS, or Linux.

✅ 1. Install a C Compiler
First, make sure you have a compiler like g (part of GCC) installed:
- Windows : Install MinGW-w64 or use MSYS2
- macOS : Install Xcode Command Line Tools:
xcode-select --install
- Linux : Install build-essential (Ubuntu/Debian):
sudo apt install build-essential
Verify installation:

g --version
✅ 2. Create a Custom Build System in Sublime Text
In Sublime Text, go to:
Tools → Build System → New Build System...
Replace the default content with this (for C):
{ "cmd": ["g ", "-std=c 17", "-Wall", "$file", "-o", "$file_base_name"], "file_regex": "^(..[^:]*):([0-9] ):([0-9] ): (.*)$", "working_dir": "$file_path", "selector": "source.c , source.cpp", "variants": [ { "name": "Run", "shell": true, "cmd": "g -std=c 17 -Wall '$file' -o '$file_base_name' && ./'$file_base_name'" } ] }
? Save it as C .sublime-build
(Sublime will auto-save it in the right folder).
✅ 3. How to Use It
- Open a
.cpp
file (eg,hello.cpp
) - Press
Ctrl B
(orCmd B
on macOS) → This compiles only - Press
Ctrl Shift B
→ Choose "Run" → This compiles and runs
Example hello.cpp
:
#include <iostream> int main() { std::cout << "Hello from Sublime Text!\n"; return 0; }
After running, output appears in Sublime's output panel at the bottom.
? Tips:
- Use
-std=c 17
or higher for modern C -
-Wall
enables helpful warnings - If you get "Access is denied" on Windows, make sure your antivirus or shell isn't blocking execution
- For debugging, consider using an IDE like VS Code — but for quick edits and runs, Sublime g is fast and clean
That's it! You can now write, compile, and run C in Sublime Text with just two keystrokes. No terminal needed unless you want input — in which case, use "shell": true
and run in terminal (you can tweak the build system for that too).
The above is the detailed content of How to compile and run C in Sublime Text. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

C folderexpressions is a feature introduced by C 17 to simplify recursive operations in variadic parameter templates. 1. Left fold (args...) sum from left to right, such as sum(1,2,3,4,5) returns 15; 2. Logical and (args&&...) determine whether all parameters are true, and empty packets return true; 3. Use (std::cout

The answer is: Use the std::string constructor to convert the char array to std::string. If the array contains the intermediate '\0', the length must be specified. 1. For C-style strings ending with '\0', use std::stringstr(charArray); to complete the conversion; 2. If the char array contains the middle '\0' but needs to convert the first N characters, use std::stringstr(charArray,length); to clearly specify the length; 3. When processing a fixed-size array, make sure it ends with '\0' and then convert it; 4. Use str.assign(charArray,charArray strl

The most common method of finding vector elements in C is to use std::find. 1. Use std::find to search with the iterator range and target value. By comparing whether the returned iterator is equal to end(), we can judge whether it is found; 2. For custom types or complex conditions, std::find_if should be used and predicate functions or lambda expressions should be passed; 3. When searching for standard types such as strings, you can directly pass the target string; 4. The complexity of each search is O(n), which is suitable for small-scale data. For frequent searches, you should consider using std::set or std::unordered_set. This method is simple, effective and widely applicable to various search scenarios.

The system endianness can be detected by a variety of methods, the most commonly used is the union or pointer method. 1. Use a union: Assign uint32_t to 0x01020304, if the lowest address byte is 0x04, it is a small endian, and if it is 0x01, it is a big endian; 2. Use pointer conversion: Assign uint16_t to 0x0102, read the byte order through the uint8_t pointer, [0]==0x02 and [1]==0x01 is a small endian, otherwise it is a big endian; 3. Compilation-time detection: define the constexpr function to determine whether the (char)&int variable is 1, and combine ifconstexpr to determine the endian order during the compilation period; 4. Runtime macro encapsulation: use (char*)&

TodebugaC applicationusingGDBinVisualStudioCode,configurethelaunch.jsonfilecorrectly;keysettingsincludespecifyingtheexecutablepathwith"program",setting"MIMode"to"gdb"and"type"to"cppdbg",using"ex

std::mutex is used to protect shared resources to prevent data competition. In the example, the automatic locking and unlocking of std::lock_guard is used to ensure multi-thread safety; 1. Using std::mutex and std::lock_guard can avoid the abnormal risks brought by manual management of locks; 2. Shared variables such as counters must be protected with mutex when modifying multi-threads; 3. RAII-style lock management is recommended to ensure exception safety; 4. Avoid deadlocks and multiple locks in a fixed order; 5. Any scenario of multi-thread access to shared resources should use mutex synchronization, and the final program correctly outputs Expected:10000 and Actual:10000.

Install the g compiler (Windows uses MinGW-w64, macOS runs xcode-select--install, and Linux executes sudoaptinstallbuild-essential); 2. Create a C.sublime-build file in SublimeText and fill in the specified JSON configuration; 3. After opening the .cpp file, press Ctrl B to compile, press Ctrl Shift B to select Run to compile and run, and the output results will be displayed in the bottom panel.

Install the Boost library, 2. Write code for DNS resolution using Boost.Asio, 3. Compile and link the boost_system library, 4. Run the program to output the IP address parsed by www.google.com; this example shows how Boost.Asio simplifies network programming in C, implements cross-platform, type-safe synchronous DNS queries through io_context and tcp::resolver, and supports IPv4 and IPv6 address resolution, and finally prints all resolution results.
