Gentlemen, the situation is this:
A file with the extension .cpp is sent to the server. The task of the engine is to compile the file, and, if there are no errors, execute it.
I ask for advice, in what depths I dig, maybe someone knows articles on this topic, or something else. All information, any, even on trifles, is welcomed.
2 answers
First, we read about how to run programs using php and get output .
Next, in order to compile the file, you need to download it (there are many articles on this topic) or send it as a post text request. And then run a command like g++ $filename -o $output . g ++ returns the response codes, so you can find out from them whether the compilation is successful (most likely, like any normal linux program, it will return 0 if successful). But in any case, you can check whether the file was generated along the path of $output (it was transferred during compilation).
Actually, in php, you can run external binaries and with the help of backward quotes.
And the launch ... and it is no different from compiling. In the same way, everything starts. The path to the file being launched is known.
- about the team can be more? I'm lost with the phrase "like g ++ $ filename -o $ output. g ++" - Arc
- one
g++is a compiler for the c ++ language, which is easily installed on almost any Linux distribution. (yes, such an interesting name with two pluses). I strongly recommend to concur at least a primitive helloworld. Otherwise, all further work will be meaningless. Look closely, the team is a bit shorter than you thought. g ++ $ filename -o $ output $ filename is the name of the file with the path you want to compile (source). $ output is the path + file name where the compiler will place the finished binary (if it succeeds). - KoVadim
The compilation process itself is quite simple: you just need to start the compiler from the command line (for example, GCC; by itself, it should be installed on the server) and transfer the necessary file to it. It is also easy to get the output of the compiler, check it for error messages and run the file obtained after compilation. All of the above is easy to turn using the system() function. But there is a serious problem - security. Nothing prevents the user from writing code that deletes important files on the server. Therefore, you need to organize a sandbox in which the application will run. Either run the program in a virtual machine, which is not a bad thing to spoil and which can be easily re-launched in automatic mode, or run the program on behalf of the user with the most reduced rights.
- "All of the above is easy to crank using the system () function." And you can at least an article ... or just explain, how? Suppose I have a compiler - MinGW (simple, convenient, free) or some other thread is not the point, so how can you tell it that you run this file for me, and please see the result here.? - Arc
- Read the documentation of your compiler, it describes the process of running it from the command line. Your task is to form a command to start the compiler, pass it to the
systemfunction and save the value returned to it (output from your compiler). In the output you can find error messages or successful compilation. - fori1ton