+ Responder ao Tópico

C



  1. #1

    Padrão C

    eu tenho esse script em C
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/io.h>
    #include <unistd.h>

    typedef char byte;

    /*
    Port addresses (&acute;dmesg | grep parport0&acute
    0x3bc = lp0 0x378 = lp1 0x278 = lp2
    **/
    #define BASEPORT 0x378
    #define DATA BASEPORT+0
    #define STATUS BASEPORT+1
    #define CONTROL BASEPORT+2

    /*
    Function headers
    **/
    byte isbitset (byte val, byte bit);
    byte setbit (byte val, byte bit);

    /*
    And... here it is... The main part...
    **/
    int main (int argc, char *argv[])
    {
    byte signal=setbit(0, 0);
    byte data=0, status=0, control=0;

    if (argc == 1)
    {
    printf("PPSW - Parallel Port Switcher\n&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    printf("by Herbert G. Fischer 2001\n&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    printf("GPL license (http://www.gnu.org/licenses/gpl.html)\n\n&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    printf("ppsw [cmd] &\n&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    printf("[cmd] - full path to application to run\n\n&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    exit(1);
    }

    // get permissions to use I/O ports
    if (ioperm(BASEPORT, 3, 1))
    {
    perror("ioperm&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    exit(1);
    }

    while (1)
    {
    // send a bit to port
    outb(data, DATA);
    // get response from input
    status = inb(STATUS);
    if (isbitset(status, 4))
    {
    // run the specified program
    if (!system(argv[1]))
    exit(1);
    else
    exit(0);
    }
    // wait 50ms to loop again
    usleep(50000);
    }

    // free I/O permissions
    if (ioperm(BASEPORT, 3, 0))
    {
    perror("ioperm&quot<IMG SRC="images/forum/icons/icon_wink.gif">;
    exit(1);
    }

    return 0;
    }

    // is the &acute;bit&acute; set on &acute;val&acute;?
    byte isbitset (byte val, byte bit)
    {
    byte bitpos[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
    return (val & bitpos[bit]);
    }

    mas qdo vou compilar
    gcc -02 ppsw.c -o ppsw
    ele da esse erro:/tmp/ccf0Egmj.o: In function `main&acute;:
    /tmp/ccf0Egmj.o(.text+0x14): undefined reference to `setbit&acute;
    collect2: ld returned 1 exit status

  2. #2
    Visitante

    Padrão C

    <TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1>Quote:</font><HR></TD></TR><TR><TD><FONT SIZE=-1><BLOCKQUOTE>
    On 2002-12-04 15:19, 1c3_m4n wrote:
    eu tenho esse script em C
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/io.h>
    #include <unistd.h>

    typedef char byte;

    /*
    Port addresses (&acute;dmesg | grep parport0&acute
    0x3bc = lp0 0x378 = lp1 0x278 = lp2
    **/
    #define BASEPORT 0x378
    #define DATA BASEPORT+0
    #define STATUS BASEPORT+1
    #define CONTROL BASEPORT+2

    /*
    Function headers
    **/
    byte isbitset (byte val, byte bit);
    byte setbit (byte val, byte bit);

    /*
    And... here it is... The main part...
    **/
    int main (int argc, char *argv[])
    {
    byte signal=setbit(0, 0);
    byte data=0, status=0, control=0;

    if (argc == 1)
    {
    printf("PPSW - Parallel Port Switcher\n"-);
    printf("by Herbert G. Fischer 2001\n"-);
    printf("GPL license (http://www.gnu.org/licenses/gpl.html)\n\n"-);
    printf("ppsw [cmd] &\n"-);
    printf("[cmd] - full path to application to run\n\n"-);
    exit(1);
    }

    // get permissions to use I/O ports
    if (ioperm(BASEPORT, 3, 1))
    {
    perror("ioperm"-);
    exit(1);
    }

    while (1)
    {
    // send a bit to port
    outb(data, DATA);
    // get response from input
    status = inb(STATUS);
    if (isbitset(status, 4))
    {
    // run the specified program
    if (!system(argv[1]))
    exit(1);
    else
    exit(0);
    }
    // wait 50ms to loop again
    usleep(50000);
    }

    // free I/O permissions
    if (ioperm(BASEPORT, 3, 0))
    {
    perror("ioperm"-);
    exit(1);
    }

    return 0;
    }

    // is the &acute;bit&acute; set on &acute;val&acute;?
    byte isbitset (byte val, byte bit)
    {
    byte bitpos[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
    return (val & bitpos[bit]);
    }

    mas qdo vou compilar
    gcc -02 ppsw.c -o ppsw
    ele da esse erro:/tmp/ccf0Egmj.o: In function `main&acute;:
    /tmp/ccf0Egmj.o(.text+0x14): undefined reference to `setbit&acute;
    collect2: ld returned 1 exit status

    O Código correto para efetuar esta tarfa está logo abaixo o que aconteceu é que a variavel foi declarada e não foi executada no final do programa. Também estava faltando executar uma ação com ; (ponto e vírgula. Executado com a ajuda de Jairo (fera em programação em C) e testado do por Marcelo Falcão.

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

    typedef char byte;

    /*
    * Port addresses (&acute;dmesg | grep parport0&acute
    * 0x3bc = lp0 0x378 = lp1 0x278 = lp2
    * **/
    #define BASEPORT 0x378
    #define DATA BASEPORT+0
    #define STATUS BASEPORT+1
    #define CONTROL BASEPORT+2

    /*
    * Function headers
    * **/
    byte isbitset (byte val, byte bit);
    byte setbit (byte val, byte bit);

    /*
    * And... here it is... The main part...
    * **/
    int main (int argc, char* argv[])
    {
    byte signal=setbit(0,0);
    byte data=0, status=0, control=0;
    if (argc == 1)
    {
    printf("PPSW - Parallel Port Switcher\n");
    printf("by Herbert G. Fischer 2001\n");
    printf("GPL license (http://www.gnu.org/licenses/gpl.html)\n\n");
    printf("ppsw [cmd] &\n");
    printf("[cmd] - full path to application to run\n\n");
    exit(1);
    }
    // get permissions to use I/O ports
    if (ioperm(BASEPORT, 3, 1))
    {
    perror("ioperm");
    exit(1);
    }
    while (1)
    {
    // send a bit to port
    outb(data, DATA);
    // get response from input
    status = inb(STATUS);
    if (isbitset(status, 4))
    {
    // run the specified program
    if (!system(argv[1]))
    exit(1);
    else
    exit(0);
    }
    // wait 50ms to loop again
    usleep(50000);
    }
    // free I/O permissions
    if (ioperm(BASEPORT, 3, 0))
    {
    perror("ioperm");
    exit(1);
    }
    return 0;
    }
    //is the &acute;bit&acute; set on &acute;val&acute;?
    byte isbitset (byte val, byte bit)
    {
    byte bitpos[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
    return (val & bitpos[bit]);
    }
    byte setbit (byte val, byte bit)
    {
    byte bitpos[] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
    return (val & bitpos[bit]);
    }


    </BLOCKQUOTE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE>