Could you guide me to do the following if it doesn't bother you: I'm making a C++ Editor in C# , but I'm looking for a way that it can compile the C++ code through g++ from the CMD, but so far what I've Researched hasn't helped me much, so far this is what I have:
This part of the code is the process of compiling a file since I need to generate the .EXE , .O and .S if you notice it ExecuteCommand();
is to enter the command
private void MemoriaToolStripMenuItem_Click(object sender, EventArgs e)
{
/*
* Ahora para compilar en memoria tenemos que llevar a cabo el siguiente proceso, primeramente para que el
* codigo fuente pueda ser compilado necesita guardarse en un archivo con la extension .cpp, sabiendo que
* el codigo fue creado en nuestro programa ya que para eso esta la otra opcion de compilacion en disco, es asi
* que en este proceso es lo que se necesita para poder compilar.
*/
if(Direccion_archivo==null)
{
MessageBox.Show("El Archivo no ha sido guardado, por favor hagalo antes de compilar");
}
else
{
//empieza el proceso de compilacion
//Generamos 3 archivos
// -> .exe "Ejecutable"
// -> .o "Codigo objeto"
// -> .s "Codigo Ensamblador"
ExecuteCommand("cd " + Dir_raiz);
//Terminal.ClearSelected();
ExecuteCommand("g++ -v " + Direccion_archivo + " -o " + Direccion_archivo.Replace(".cpp", ".exe"));
//System.Threading.Thread.Sleep(3000);
ExecuteCommand("g++ -v " + Direccion_archivo + " -o " + Direccion_archivo.Replace(".cpp", ".o"));
//Generamos el codigo Ensamblador Resultado de el siguiente comando
ExecuteCommand("g++ -S -masm=intel " + Direccion_archivo);
codigo_asm = File.ReadAllText(Direccion_archivo.Replace(".cpp", ".s"));
Salida_Assembler.Clear();
Salida_Assembler.Text = codigo_asm;
flag_execute = true;
}
}
In this part here is the method to execute the CMD commands, because I am somewhat inexperienced in the use of the CMD implemented in a language, I did some research and found the routines contained in this function of the Code Tricks Blog , but the problem is that it opens a new console every time I implement the function ExecuteCommand()
and after checking many times it is not able to generate any compilation file for any reason, I think that I should implement a thread so that the console stays open and pass the commands but because I do not know about the subject and I have investigated but I have not found much information about it; could you help me to implement a solution to this problem.
private void ExecuteCommand(String _Command)
{
/*
* Lo que necesitamos es hacer uso de la cmd para poder compilar el codigo que nosostros pongamos en el
* editor de texto, pero antes que nada debemos de tener instalado el compilador en este caso como vamos a
* utilizar c++ y c como lenguaje base para compilar estamos obligados a utilizar Gcc pero ya que este solo
* esta disponible a traves de MinGW haremos uso de este programa y tambien por problemas de Compatibilidad
* en la arquitectura x86_64 haremos uso de Posix rev para tener compatibilidad en la arquitectura en la que
* estamos trabajando.
*/
Process cmdProcess = null;
cmdProcess = new Process();
// Dump all info from ipconfig command
cmdProcess.StartInfo.Arguments = "/ALL";
// Run command: ipconfig
cmdProcess.StartInfo.FileName = _Command;
// Redirect stdout
cmdProcess.StartInfo.RedirectStandardOutput = true;
// Set to false, otherwise you can't redirect stdout
cmdProcess.StartInfo.UseShellExecute = false;
// Start process
if (cmdProcess.Start())
{
// Read stdout and show the content in a rtf-box
Terminal.Text = cmdProcess.StandardOutput.ReadToEnd();
}
else
{
// Failed to execute command
}
}
I would really appreciate your help
PS: The commands to compile GCC have not given me any problem in the CMD, it should be noted that I have been testing them without any problem from the console, in turn I am using MinGW as a compiler.
After a long time, and not getting an answer about the solution, I myself was able to design a convincing and fully functional solution to my problem, for what I needed, in which it is quite simple.
This is the process that helps me to compile the program, in fact the idea is simple, to what it is to have a .bat file called Daemon.bat stored in the folder where the program is stored and what this function simply does is change change the extensions of the program that I have open in the program and previously saved or overwritten and through the function
Replace()
we can change the extensions of the file that we are going to compile since we need the executable, the object code and the assembly code of the program.Previously for this we must have the address of the Daemon.bat file in which I have it stored in the string type variable
Dir_demon
.Now to be able to execute this process we simply call it and enter the argument, which in this case is the address where the program that we are going to compile is saved, in this case I have the address as well as I have it stored in a variable called
Direccion_Disk
and to execute the processCrearDemonio()
is like this:And finally, to be able to execute the batch file that we modify at program execution time, we help ourselves through a Visual Basic script that helps us keep the console hidden and executes it in the background.
Finally in the program and only we send a call through the following function:
We sleep the program for a short period of time because the compiler takes a while to process the object code, asm code, and the executable. It should be noted that the script is in the same folder as the batch file, so I use the
Replace()
.Well, in short, this is the solution that was very useful for me and to date it continues to work and has not given me any problems, I hope it will help you or can give you some idea on how to solve a similar problem, Greetings.