@00sukhjeet00/compilerjs 中文文档教程

发布于 3年前 浏览 19 项目主页 更新于 3年前

CompilerJs

CompilerJs 是 Node JS 库,用于为 C/C++、Java、Python 等编程语言编译代码。
支持Mac OS(超时功能未实现)、Windows和Linux系统。

Setting Up Compilers

为了编译任何编程语言,您需要首先在服务器机器上安装该编程语言的编译器。

C and C++

  1. Installation :You need GCC compiler that can compile programs from your cmd/terminal
    • Windows - You can get MinGw .
    • Linux - Most of the linux versions are installed with gcc by default. If you haven't got , Take a look at Installing GCC .
  2. Testing the Environment :After installing , set your environment variables for accessing the GCC command lines from any directory
    • Windows - create a c file in a directory , execute
      g++ filename.c -o output.exe
      output.exe

      then you will get the output of the program
    • Linux - create a c file in a directory , execute
      gcc filename.c -o output.out
      ./output.out

      then you will get the output of the program

Java

  1. Installion : You need JDK ( Java Development Kit ) to compile Java programs.Click here to download JDK for various platforms.
  2. Testing the Environment :After installing , set your environment variables for accessing the javac command lines from any directory
    • Create a Java file named Main.java with main function
      javac Main.java
      java Main

      then you will get the output of the program.

Python

  1. Installation : You can get and install Python from here
  2. Testing the Environment :After installing , set your environment variables for accessing python command lines from any directory
    • Create a python file hello.py and execute
      python hello.py
      then you will get the output of the program.

Documentation

1)Require compilerJs
const compilerJs = require('@00sukhjeet00/compilerjs');
compilerJs.init();

init() 在您的项目目录中创建一个名为 code 的文件夹,用于存储目的。 在使用其他方法之前,请务必调用 init() 方法。

2)C and C++
    //Implementation for windows  
    const envData = { ext : "g++",options:{timeout:5000}} // (uses g++ command to compile )
    //Implementation for Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "gcc", options:{timeout:5000} } // ( uses gcc command to compile )
    compilerJs.compileCPP(envData , code ,(data)=> {
        console.log(data)
        //data.error = error message 
        //data.output = output value
    })
3)C and C++ with inputs
    //Implementation for windows Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "g++",options:{timeout:5000}} // (uses g++ command to compile )

    //Implementation for  Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "gcc" ,options:{timeout:5000}}; // ( uses gcc command to compile )

    compilerJs.compileCPPWithInput(envData , code , input , (data)=> {
           console.log(data);
    });
4)Java
    var envData = { ext:"java" ,options:{timeout:5000} }; 
    compilerJs.compileJava( envData , code ,(data)=>{
        console.log(data);
    });    
5)Java with inputs
    const envData = { ext:"java" ,options:{timeout:5000} }; 
    compilerJs.compileJavaWithInput( envData , code , input ,(data)=>{
        console.log(data);
    });
6)Python
    const envData = { ext:"py" ,options:{timeout:5000} }; 
    compilerJs.compilePython( envData , code ,(data)=>{
        console.log(data);
    });    
7)Python with inputs
    const envData = { ext:"py" options: {timeout:5000} }; 
    compilerJs.compilePythonWithInput( envData , code , input ,(data)=>{
        console.log(data);        
    });

Timeout functionality

超时有助于在特定时间内(以毫秒为单位)运行程序。 它支持window和linux系统。 超时可以在 C/C++、Java、Python 中类似地使用,如下所示。

    const envData={ ext: "py", options: {timeout:5000} } // timeout: 5 running program for 5 sec.

Demo Code

Python:

    const compilerJS = require('@00sukhjeet00/compilerjs')
    compilerJs.init()
    const envData = { ext: 'py', options: { timeout: 1 } }
    const code=`print('hello')`
    compilerJs.compilePy(envData, code, (data) => {
            if (data.error)
                console.log(data.error)
            else
        {
                if (data.timeout)
                        console.log('TLE')
                else
                        console.log(data.out)
            }
    })

其他语言的类似代码结构

CompilerJs

CompilerJs is Node JS library use to compile code for programing languages like C/C++, Java, Python.
Support Mac OS(Timeout functionality is not implemented), Windows and Linux System.

Setting Up Compilers

Inorder to compile any programming language , you need to first have the compiler for that programming language in the server machine.

C and C++

  1. Installation :You need GCC compiler that can compile programs from your cmd/terminal
    • Windows - You can get MinGw .
    • Linux - Most of the linux versions are installed with gcc by default. If you haven't got , Take a look at Installing GCC .
  2. Testing the Environment :After installing , set your environment variables for accessing the GCC command lines from any directory
    • Windows - create a c file in a directory , execute
      g++ filename.c -o output.exe
      output.exe

      then you will get the output of the program
    • Linux - create a c file in a directory , execute
      gcc filename.c -o output.out
      ./output.out

      then you will get the output of the program

Java

  1. Installion : You need JDK ( Java Development Kit ) to compile Java programs.Click here to download JDK for various platforms.
  2. Testing the Environment :After installing , set your environment variables for accessing the javac command lines from any directory
    • Create a Java file named Main.java with main function
      javac Main.java
      java Main

      then you will get the output of the program.

Python

  1. Installation : You can get and install Python from here
  2. Testing the Environment :After installing , set your environment variables for accessing python command lines from any directory
    • Create a python file hello.py and execute
      python hello.py
      then you will get the output of the program.

Documentation

1)Require compilerJs
const compilerJs = require('@00sukhjeet00/compilerjs');
compilerJs.init();

init() creates a folder named code in your project directory which is used for storage purpose. Before using other methods , make sure to call init() method.

2)C and C++
    //Implementation for windows  
    const envData = { ext : "g++",options:{timeout:5000}} // (uses g++ command to compile )
    //Implementation for Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "gcc", options:{timeout:5000} } // ( uses gcc command to compile )
    compilerJs.compileCPP(envData , code ,(data)=> {
        console.log(data)
        //data.error = error message 
        //data.output = output value
    })
3)C and C++ with inputs
    //Implementation for windows Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "g++",options:{timeout:5000}} // (uses g++ command to compile )

    //Implementation for  Linux and Mac OS(Timeout functionlity is not implemented)
    const envData = { ext : "gcc" ,options:{timeout:5000}}; // ( uses gcc command to compile )

    compilerJs.compileCPPWithInput(envData , code , input , (data)=> {
           console.log(data);
    });
4)Java
    var envData = { ext:"java" ,options:{timeout:5000} }; 
    compilerJs.compileJava( envData , code ,(data)=>{
        console.log(data);
    });    
5)Java with inputs
    const envData = { ext:"java" ,options:{timeout:5000} }; 
    compilerJs.compileJavaWithInput( envData , code , input ,(data)=>{
        console.log(data);
    });
6)Python
    const envData = { ext:"py" ,options:{timeout:5000} }; 
    compilerJs.compilePython( envData , code ,(data)=>{
        console.log(data);
    });    
7)Python with inputs
    const envData = { ext:"py" options: {timeout:5000} }; 
    compilerJs.compilePythonWithInput( envData , code , input ,(data)=>{
        console.log(data);        
    });

Timeout functionality

Timeout help to run program for perticular time (in ms). It support window and linux system. Timeout can be used similarly in C/C++, Java, Python as showen below.

    const envData={ ext: "py", options: {timeout:5000} } // timeout: 5 running program for 5 sec.

Demo Code

Python:

    const compilerJS = require('@00sukhjeet00/compilerjs')
    compilerJs.init()
    const envData = { ext: 'py', options: { timeout: 1 } }
    const code=`print('hello')`
    compilerJs.compilePy(envData, code, (data) => {
            if (data.error)
                console.log(data.error)
            else
        {
                if (data.timeout)
                        console.log('TLE')
                else
                        console.log(data.out)
            }
    })

Similar code structure for other languages

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文