c-compiler
is a simple Node.js-based command-line tool designed to compile C programs using the gcc
compiler. It allows you to compile .c
files from your local system or fetch and compile C code directly from a web URL. This makes it versatile for developers who want to test code stored online or compile local projects quickly.
To use c-compiler
, you need to have Node.js and gcc
installed on your system. Follow these steps:
- Install Node.js: Download and install Node.js from nodejs.org if you don’t already have it.
-
Install GCC: Ensure
gcc
is installed on your system.- On Linux:
sudo apt install gcc
(Ubuntu/Debian) orsudo yum install gcc
(CentOS/RHEL). - On macOS: Install via Xcode command-line tools (
xcode-select --install
) or Homebrew (brew install gcc
). - On Windows: Install MinGW or a similar GCC-compatible compiler and ensure
gcc
is in your PATH.
- On Linux:
-
Install c-compiler: Run the following command to install the tool globally:
npm install -g c-compiler
Use the compile-c
command to compile C code from either a local file or a web URL.
compile-c -f <filename>
- Replace
<filename>
with the name of your C file (e.g.,hello.c
). - The tool will compile the file into an executable with the same name, minus the
.c
extension (e.g.,hello
).
- Create a file named
hello.c
:#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
- Run the command:
compile-c -f hello.c
- If successful, you’ll see:
Compiling hello.c... Compilation successful! Executable created: hello
- Run the executable:
- On Linux/macOS:
./hello
- On Windows:
hello.exe
Output:Hello, World!
- On Linux/macOS:
compile-c -u <url>
- Replace
<url>
with a URL pointing to raw C code (e.g., a GitHub raw file link). - The tool will fetch the code, compile it, and create an executable named
output
.
- Use a URL like
https://raw.githubusercontent.com/user/repo/main/hello.c
containing:#include <stdio.h> int main() { printf("Hello from the web!\n"); return 0; }
- Run the command:
compile-c -u https://raw.githubusercontent.com/user/repo/main/hello.c
- If successful, you’ll see:
Compiling code from URL... Compilation successful! Executable created: output
- Run the executable:
- On Linux/macOS:
./output
- On Windows:
output.exe
Output:Hello from the web!
- On Linux/macOS:
- If no file or URL is specified:
Error: Please specify a C file (-f) or a URL (-u)
- If both file and URL are specified:
Error: Please specify either a file (-f) or a URL (-u), not both
- If the local file doesn’t exist:
Error: File "nonexistent.c" does not exist
- If the URL fails to load:
Error: Failed to fetch C code from URL "<url>" <error message>
- If compilation fails:
Compilation failed: <gcc error output>