c-compiler

1.3.0 • Public • Published

c-compiler

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.

Installation

To use c-compiler, you need to have Node.js and gcc installed on your system. Follow these steps:

  1. Install Node.js: Download and install Node.js from nodejs.org if you don’t already have it.
  2. Install GCC: Ensure gcc is installed on your system.
    • On Linux: sudo apt install gcc (Ubuntu/Debian) or sudo 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.
  3. Install c-compiler: Run the following command to install the tool globally:
    npm install -g c-compiler
    

Usage

Use the compile-c command to compile C code from either a local file or a web URL.

Compile from a Local File

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).

Example

  1. Create a file named hello.c:
    #include <stdio.h>
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
  2. Run the command:
    compile-c -f hello.c
    
  3. If successful, you’ll see:
    Compiling hello.c...
    Compilation successful!
    Executable created: hello
    
  4. Run the executable:
    • On Linux/macOS: ./hello
    • On Windows: hello.exe Output: Hello, World!

Compile from a Web URL

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.

Example

  1. 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;
    }
  2. Run the command:
    compile-c -u https://raw.githubusercontent.com/user/repo/main/hello.c
    
  3. If successful, you’ll see:
    Compiling code from URL...
    Compilation successful!
    Executable created: output
    
  4. Run the executable:
    • On Linux/macOS: ./output
    • On Windows: output.exe Output: Hello from the web!

Error Handling

  • 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>
    

Package Sidebar

Install

npm i c-compiler

Weekly Downloads

11

Version

1.3.0

License

MIT

Unpacked Size

5.87 kB

Total Files

3

Last publish

Collaborators

  • safwanganz