Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Compilar un codigo fuente de linux para Windows se puede?

Estas en el tema de Compilar un codigo fuente de linux para Windows se puede? en el foro de C/C++ en Foros del Web. Voy a tratar de ser lo mas claro posible. Siempre ando por el foro pero en el apartado PHP, en este caso necesito de vuestro ...
  #1 (permalink)  
Antiguo 05/10/2012, 23:06
 
Fecha de Ingreso: diciembre-2001
Ubicación: Mar del Plata
Mensajes: 203
Antigüedad: 22 años, 4 meses
Puntos: 0
Compilar un codigo fuente de linux para Windows se puede?

Voy a tratar de ser lo mas claro posible.
Siempre ando por el foro pero en el apartado PHP, en este caso necesito de vuestro conocimiento y ayuda si fuera posible.
Tengo una pagina php que interactua con un archivo.exe que lee y escribe en el puerto paralelo del servidor, todo funciona muy bien salo por el echo que solo puedo interactuar con los 8 bits de registros de datos es decir las salidas hacie el puerto, pero este tiene 5 entradas por las que se mandan datos que necesito leer y el archivo.exe en cuestion solo trabaja con las 8 salidas nada mas, esto corroborado. Consegi el fuente de este archivo.exe pero en su version original puesto que fue desarrollado para linux y luego portado a windows pero con la falencia que les cuento, mi idea es en base al fuente poder compilarlo nuevamente con la correccion del caso nuevamente en un archivomejorado.exe para poder usarlo con php, apache en windows xp por ejemplo.


Todo se compone de:
archivo.php
archivo.exe
Sajax.php
inpout32.dll

Les dejo el fuente del archivo para linux que quiero portar y tambien el php que uso:

Código:
/*
 * Simple general purpose I/O port control program for Linux
 * Written and copyright by Tomi Engdahl 2005
 * (e-mail: [email protected])
 *
 *
 * Supported port identifiers
 *  LPT1DATA
 *  LPT1STATUS
 *  LPT1HANDSHAKE
 *  JOYSTICK
 *   
 *
 * Supported commands
 *
 *  PRINT DEC       Read data, gives output as decimal number
 *  PRINT HEX       Read data, gives output as hexadecimal number
 *  PRINT BIN       Read data, gives output as binary number
 *  PRINTBITS bits  Reads the specified bits in the specified order (0..7)
 * 
 *  WRITE           Writes register value to port
 *  READ            Reads value from specified port to register
 *
 *  SETVALUE value  Sets the given value to register
 *  AND value       Performs AND with given value and register
 *  OR value        Performs OR with given value and register
 *  XOR value       Performs XOR with given value and register
 *  SETBITS bits    Sets given bits to 1 in register
 *  RESETBITS bits  Sets given bits to 0 in register
 *  TOGGLEBITS bits Toggles specified bit values  
 *
 * value can be gives as decimal number or heaxadecimal starting with 0x
 * bits is a list of bit position identifiers from 0 to 7
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>

#define MAX_ARGUMENT_LEN 8

/*
 * String compare routine, gives 1 when strings match, 0 otherwise
 */
int stringMatch(char * str1, char * str2)
{
  if (strcmp(str1,str2) == 0)
  {
    return 1; 
  }
  return 0;
}


/* 
 * Routine to convert port 
 */
int PortIDtoAddress(char * portID)
{
  if (stringMatch(portID, "LPT1DATA")) 
    {
      return 0x0378;

/* para mi el error esta aqui porque tendria que ser "return 0x0378 +1" */

    }
  if (stringMatch(portID, "LPT1STATUS"))
    {
      return (0x0379 + 1);
    }  
  if (stringMatch(portID, "LPT1HANDSHAKE"))
    {
      return (0x0378 + 2);
    }  
  if (stringMatch(portID, "JOYSTICK"))
    {
      return 0x0201;
    } 
  fprintf(stderr, "Error: Invalid port name %s\n", portID), exit(1);
  return 0;
}


int ReadPort(char * portID)
{
  int iobase;

  iobase = PortIDtoAddress(portID);
  if (iobase==0) fprintf(stderr, "Error: Invalid port address %s\n", portID), exit(1);
  if (ioperm(iobase,1,1)) 
    {
      fprintf(stderr, "Error: Couldn't get the port at %x\n", iobase), exit(1);
    }
  else
    {
      return inb(iobase);
    }
  return 0;
}

int WritePort(char * portID, int value)
{
  int base;


  base = PortIDtoAddress(portID);

  if (base==0) fprintf(stderr, "Error: Invalid port address\n", base), exit(1);
  if (ioperm(base,1,1)) 
    {
      fprintf(stderr, "Error: Couldn't get the port at %x\n", base), exit(1);
    }
  else
    {
      outb(value, base);
    }
  return 0;
}

void bin_prnt_byte(int x)
{
   int n;
   for(n=0; n<8; n++)
   {
      if((x & 0x80) !=0)
      {
         printf("1");
      }
      else
      {
         printf("0");
      }
      x = x<<1;
   }
}

int printBits(int value,char *specifier)
{
  int index=0;
  int mask=0;

  while ((index < MAX_ARGUMENT_LEN) && (specifier[index]!=0))
    {
      switch (specifier[index])
	{
	case '0':
	  if ((value & 1) != 0) printf("1"); else printf("0");
	  break;
	case '1':
	  if ((value & 2) != 0) printf("1"); else printf("0");
	  break;
	case '2':
	  if ((value & 4) != 0) printf("1"); else printf("0");
	  break;
	case '3':
	  if ((value & 8) != 0) printf("1"); else printf("0");
	  break;
	case '4':
	  if ((value & 16) != 0) printf("1"); else printf("0");
	  break;
	case '5':
	  if ((value & 32) != 0) printf("1"); else printf("0");
	  break;
	case '6':
	  if ((value & 64) != 0) printf("1"); else printf("0");
	  break;
	case '7':
	  if ((value & 128) != 0) printf("1"); else printf("0");
	  break;
	default:
	  fprintf(stderr, "Error: Wrong bit position specifier.\n"), exit(1);
	  break;
	}
      index++;
    }
  printf("\n");
  return 0;
}

/* 
 * Converts bit string like 0123456789
 */
int bitsToMask(char * specifier)
{
  int index=0;
  int mask=0;

  while ((index < MAX_ARGUMENT_LEN) && (specifier[index]!=0))
    {
      switch (specifier[index])
	{
	case '0':
	  mask = mask | 1;
	  break;
	case '1':
	  mask = mask | 2;
	  break;
	case '2':
	  mask = mask | 4;
	  break;
	case '3':
	  mask = mask | 8;
	  break;
	case '4':
	  mask = mask | 16;
	  break;
	case '5':
	  mask = mask | 32;
	  break;
	case '6':
	  mask = mask | 64;
	  break;
	case '7':
	  mask = mask | 128;
	  break;
	default:
	  fprintf(stderr, "Error: Wrong bit position specifier.\n"), exit(1);
	  break;
	}
      index++;
    }
  return mask;
}

int processCommand(char * command, char * argument, int * argcount, char * portId, int * portData)
{
  int mask;
  *argcount=1; // defualt takes on argument

  if (stringMatch(command,"read"))
    {
      *argcount=0; // this command did no use any arguments
      *portData=ReadPort(portId);
    }
  else if (stringMatch(command,"write"))
    {
      *argcount=0; // this command did no use any arguments
      WritePort(portId, *portData);
    }
  else if (stringMatch(command,"print"))
    {
      if (stringMatch(argument,"dec"))
	{
	  printf("%i\n",*portData);
	}
      else if (stringMatch(argument,"hex"))
	{
	  printf("%x\n",*portData);
	}
      else if (stringMatch(argument,"bin"))
	{
          bin_prnt_byte(*portData);  
	  printf("\n");
	}
      else 
	{
	  fprintf(stderr, "Error: Wrong PRINT argument.\n"), exit(1);
	}
    } 
  else if (stringMatch(command,"printbits"))
    {
      printBits(*portData,argument);
    }
  else if (stringMatch(command,"setvalue"))
    {
      *portData=atoi(argument);
    }
  else if (stringMatch(command,"and"))
    {
      mask=atoi(argument);
      *portData = (*portData) & mask;
    }
  else if (stringMatch(command,"or"))
    {
      mask=atoi(argument);
      *portData = (*portData) | mask;
    }
  else if (stringMatch(command,"xor"))
    {
      mask=atoi(argument);
      *portData = (*portData) ^ mask;
    }
  else if (stringMatch(command,"setbits") || stringMatch(command,"setbit"))
    {
      mask=bitsToMask(argument);
      *portData = (*portData) | mask;
    }
  else if (stringMatch(command,"resetbits") || stringMatch(command,"resetbit"))
    {
      mask=bitsToMask(argument) ^ 0xFF;
      *portData = (*portData) & mask;
    }

  else if (stringMatch(command,"togglebits") || stringMatch(command,"togglebit"))
    {
      mask=bitsToMask(argument);
      *portData = (*portData) ^ mask;
    }


  return 0;  
}

int main(int argc, char **argv)
{                    
  int value;
  int argIndex;
  int acount;
  int portData;

  char * portId;

  if (argc<2)
    fprintf(stderr, "Error: Wrong number of arguments.\n"), exit(1);
  portId=argv[1];


  portData=0;
  argIndex=2; // start from first argument after port definition
  while (argIndex < argc)
    {
      if ((argIndex+1) < argc) 
	{
	  processCommand(argv[argIndex],argv[argIndex+1],&acount,portId,&portData);
	}
      else 
	{
	  processCommand(argv[argIndex],argv[argIndex+1],&acount,portId,&portData);
	}
      argIndex=argIndex+acount+1;
    }
  return 0;
}
__________________
:) Fernando Dichiera (:
[email protected]
  #2 (permalink)  
Antiguo 05/10/2012, 23:07
 
Fecha de Ingreso: diciembre-2001
Ubicación: Mar del Plata
Mensajes: 203
Antigüedad: 22 años, 4 meses
Puntos: 0
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

y el php que uso:

Código PHP:
<?php
    
require("Sajax.php");

    function 
portstatus() {
/*        return  "Status: " . shell_exec("portcontrol.exe LPT1DATA read print bin");        */
        
return  strrev (shell_exec("portcontrol.exe LPT1DATA read print bin"));

    }

        function 
portcontrol($x$y) {
      if ((
$x >= 0) && ($x 8)) {
         if (
$y == 1)
           
shell_exec("portcontrol.exe LPT1DATA read setbit " $x " write");
         else 
           
shell_exec("portcontrol.exe LPT1DATA read resetbit " $x " write");
      }
                return 
portstatus();
        }
    
    
sajax_init();
    
// $sajax_debug_mode = 1;
    
sajax_export("portstatus");
        
sajax_export("portcontrol");
    
sajax_handle_client_request();
    
?>
<html>
<head>
    <title>Port control</title>
    <script>
    <?php
    sajax_show_javascript
();
    
?>
    
    function do_portstatus_cb(z) {
        // update status field in form
        document.getElementById("status").value = z;
    }
    
    function do_portstatus() {
        x_portstatus(do_portstatus_cb);
        setTimeout('do_portstatus();',714); // executes the next data query in every n milliseconds
    }

    function do_portcontrol_cb(z) {
        // update status field in form
        document.getElementById("status").value = z;
        }
    
    function do_portcontrol(bit,value) {
        x_portcontrol(bit,value,do_portcontrol_cb);
    }

    </script>
    
</head>
<body>


  <SCRIPT LANGUAGE="JavaScript">
<!--
do_portstatus();
// -->
  </SCRIPT>

<div align="center">

<input type="text" name="status" id="status" value="No status yet" size="19">

</div>
<table width="714" border="1" align="center" cellpadding="15" cellspacing="0">
  <tr>
    <td width="100"><div align="center">
      <input type="button" name="check9" value="Prender"
        onClick="do_portcontrol(0,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check2" value="Prender"
        onClick="do_portcontrol(1,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check3" value="Prender"
        onClick="do_portcontrol(2,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check4" value="Prender"
        onClick="do_portcontrol(3,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check5" value="Prender"
        onClick="do_portcontrol(4,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check6" value="Prender"
        onClick="do_portcontrol(5,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check7" value="Prender"
        onClick="do_portcontrol(6,1); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check8" value="Prender"
        onClick="do_portcontrol(7,1); return false;">
    </div></td>
  </tr>
  <tr>
    <td width="100"><div align="center">
      <input type="button" name="check10" value="Apagar"
        onClick="do_portcontrol(0,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check11" value="Apagar"
        onClick="do_portcontrol(1,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check12" value="Apagar"
        onClick="do_portcontrol(2,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check13" value="Apagar"
        onClick="do_portcontrol(3,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check14" value="Apagar"
        onClick="do_portcontrol(4,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check15" value="Apagar"
        onClick="do_portcontrol(5,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check16" value="Apagar"
        onClick="do_portcontrol(6,0); return false;">
    </div></td>
    <td width="100"><div align="center">
      <input type="button" name="check17" value="Apagar"
        onClick="do_portcontrol(7,0); return false;">
    </div></td>
  </tr>
  <tr>
    <td><div align="center">Bit 0:</div></td>
    <td><div align="center">Bit 1:</div></td>
    <td><div align="center">Bit 2:</div></td>
    <td><div align="center">Bit 3:</div></td>
    <td><div align="center">Bit 4:</div></td>
    <td><div align="center">Bit 5:</div></td>
    <td><div align="center">Bit 6:</div></td>
    <td><div align="center">Bit 7:</div></td>
  </tr>
</table>

<br>
<br>

<div align="center">
  <input type="button" name="check" value="Prender todos"
        onclick="do_portcontrol(0,1); do_portcontrol(1,1); do_portcontrol(2,1); do_portcontrol(3,1); do_portcontrol(4,1); do_portcontrol(5,1); do_portcontrol(6,1); do_portcontrol(7,1); ;return false;">
  <input type="button" name="check" value="Apagar todos"
        onclick="do_portcontrol(0,0); do_portcontrol(1,0); do_portcontrol(2,0); do_portcontrol(3,0); do_portcontrol(4,0); do_portcontrol(5,0); do_portcontrol(6,0); do_portcontrol(7,0); return false;">
  
  
</div>
</body>
</html>
Desde ya muchisimas gracias a quien pueda ayudarme.

PD: Olvide decir que lo mio es el HTML, PHP y MYSQL, se daran cuenta que de C y compilar nada de nada en conocimiento, si en ganas!!!
__________________
:) Fernando Dichiera (:
[email protected]
  #3 (permalink)  
Antiguo 06/10/2012, 11:35
 
Fecha de Ingreso: abril-2010
Ubicación: Rosario
Mensajes: 1.850
Antigüedad: 14 años
Puntos: 228
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

Probaste compilarlo en windows?
Por lo que hace creo que no deberia haber problemas!
  #4 (permalink)  
Antiguo 07/10/2012, 15:22
 
Fecha de Ingreso: diciembre-2001
Ubicación: Mar del Plata
Mensajes: 203
Antigüedad: 22 años, 4 meses
Puntos: 0
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

Si efectivamente intenté compilarlo en windows y siempre me da errores relacionados con sys/io.h , entiendo que esta es una librería de linux (en el codigo fuente que postee esta referenciada) que en windows es reemplazada por inpout32.dll pero hasta allí llego y no puedo avanzar!

Como mencioné anteriormente no tengo conocimientos de C y de compilar!
Es por eso el pedido de ayuda!
__________________
:) Fernando Dichiera (:
[email protected]

Última edición por fermdp; 07/10/2012 a las 20:09
  #5 (permalink)  
Antiguo 09/10/2012, 11:56
 
Fecha de Ingreso: diciembre-2001
Ubicación: Mar del Plata
Mensajes: 203
Antigüedad: 22 años, 4 meses
Puntos: 0
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

No se si sirve a modo informativo pero los intentos de compilar los estoy efectuando con MinGW.

Alguna sugerencia?

Desde ya muchas gracias!
__________________
:) Fernando Dichiera (:
[email protected]
  #6 (permalink)  
Antiguo 10/10/2012, 04:03
 
Fecha de Ingreso: junio-2010
Ubicación: Madrid
Mensajes: 620
Antigüedad: 13 años, 10 meses
Puntos: 73
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

Hay compiladores en los que <io.h> está dentro de la carpeta <include/sys>, y en otros está dentro de la carpeta <include>. Comprueba la carpeta <include> de tu compilador.

Tampoco indicas qué errores te da.

Veo también que se intenta leer/escribir directamente del hardware (en MS-DOS el puerto 0x0378 correspondía a LPT1, el primer puerto paralelo). En principio, Windows no permite el acceso directo al hardware. Pero primero hay que corregir los errores que tira el compilador, y luego ver que el programa haga lo que debe.
  #7 (permalink)  
Antiguo 15/10/2012, 03:01
 
Fecha de Ingreso: octubre-2012
Mensajes: 2
Antigüedad: 11 años, 6 meses
Puntos: 0
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

Hola,

Yo soy nuevo en el foro pero intentaré aportar mi granito de arena.

Para compilar:
- La librería <io.h> en windows suele estar en la carpeta include, así que logras pasar al siguiete error sustituyendo la linea:
#include <sys/io.h> ==> #include <io.h>

- Luego te dará problemas con las cadenas string (strcmp). La solución es incluir:
#include <string.h>

- Ahora vendrá el caballo de batalla: ioperm. Para esto aún no tengo solución, nunca lo he utilizado, pero se que es de Linux para acceder al puerto. Ahora tendríamos que ver qué instrucción es la equivalente en Windows.

Estoy en ello aún.

Por cierto, hoy me he dado una vuelta por el foro, y quería dar las gracias a todos los que respondéis a los foreros.
¡Muchas gracias!

Un saludo.
  #8 (permalink)  
Antiguo 22/10/2012, 04:12
 
Fecha de Ingreso: octubre-2012
Mensajes: 2
Antigüedad: 11 años, 6 meses
Puntos: 0
Respuesta: Compilar un codigo fuente de linux para Windows se puede?

Hola ¡Otra vez!

Creo que tendría que haber hecho más caso a FW190 xD

En principio, Windows no permite el acceso directo al hardware.

No soy capaz de hacerlo compilar.

Si alguién puede continuar, yo tengo curiosidad.

Salu2!!!

Etiquetas: compilar, fuente, funcion, int, linux, string, windows
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:26.