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.