LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Error compiling C code (https://www.linuxquestions.org/questions/linux-general-1/error-compiling-c-code-762515/)

Danteleo 10-16-2009 11:44 PM

Error compiling C code
 
Hello all,

I'm working on connecting my computer to my RC car (cheap car) and found a tutorial of someone who got this working years ago. Well, the hardware is built and connected to my parallel printer port but, the code doesn't work. It's written in C (of which I am not knowledgeable) and presents me with some errors. I've manages to fix a couple by using Google but, have come to a stopping point.

I have emailed the dev but, don't expect to hear from him given the 6 yrs since the posting. Here is the source code. I'm using Ubuntu Jaunty.

Code:

/*
* rccar.c: lpt port control of a radio controlled car
*
* MORE INFO HERE:      http://www.engr.uvic.ca/~sbowman/prog.php
*
* By:            Shaun Bowman Sept. 2003, shaun_bowman@hotmail.com
*
* Notes: This program is unlikely to be ready-2-port because of the ioctl
method
*          of changing terminal settings. This program was written so i could
*          control a tiny 10$ remote control car (and to procrastinate homework).
*          The lpt port pins (6,7,8,9) are used to control Forward, Reverse, Left
*          and Right in the controller. In addition to this program, I had to build
*          a simple circuit to bypass the buttons on the remote control. This is
*          because even in the low state, the Control and Ground pins still form a
*          complete circuit.
*
*          Crude Diagram (1 for each button):
*
*          GrndPin(18-25)  PinX(5-9)
*                |                |                          T1: NPN transistor, i used 2N222
*                |                |
*                |                L---R1--R2--R3                R1-R3: Resistors, i didn't have
*                |        T1:                    |                      ones with high enough
*                |--------|\                    |                resistance, so i had to use
*                |        | ------------------                  3. Total R ~= 100k ohm's
*              |    |---|/(<-collector pin)
*                |    |
*      -ButtnLead  +ButtnLead
*
* Compile with: gcc -O2 -o rccar rccar.c (must be root)
*
* Pin bits (numbered at female end)
*  outb(8, x)  = pin 5      UNUSED
*  outb(16, x)  = pin 6      Forward
*  outb(32, x)  = pin 7      Reverse
*  outb(64, x)  = pin 8      Left
*  outb(128, x) = pin 9      Right
*  outb(80, x)  = pin 6 & 8  FORWARDLEFT
*  outb(144, x) = pin 6 & 9  FORWARDRIGHT
*  outb(96, x)  = pin 7 & 8  REVERSELEFT
*  outb(160, x) = pin 7 & 9  REVERSERIGHT
*/

#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#include <string.h>
#include <libio.h>

#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

#define LPT1PORT 0x378                                                                /* lpt1 */

int main()
{

struct termios oldT, newT;                                                        /* create our termios identifiers, uphere because C++ is retarted*/

void forward( char key ){
        outb(16, LPT1PORT);                                                        /* Set pin 9 to high */
        usleep(10000);                                                                /* Sleep for a while (100 ms) */
}

void reverse( char key ){
        outb(32, LPT1PORT);
        usleep(10000);
}

void left( char key ){
        outb(64, LPT1PORT);
        usleep(10000);
}

void right( char key ){
        outb(128, LPT1PORT);
        usleep(10000);
}

void forwardleft( char key ){
        outb(80, LPT1PORT);
        usleep(10000);
}

void forwardright( char key ){
        outb(144, LPT1PORT);
        usleep(10000);
}

void reverseleft( char key ){
        outb(96, LPT1PORT);
        usleep(10000);
}

void reverseright( char key ){
        outb(160, LPT1PORT);
        usleep(10000);
}


void stop( char key ){
        outb(0, LPT1PORT);                                                        /* Set the data signals (D0-7) of the port to all low (0) */
        usleep(10000);
}

void end () {                                                                        /* The Beggining of the end.... */
        outb(0, LPT1PORT);
        usleep(10000);
        if (ioperm(LPT1PORT, 3, 0)) {perror("ioperm"); exit(1);}                /* We don't need the port anymore */
        ioctl(0,TCSETS,&oldT);                                                        /* Better set the terminal back to normal... :-) */
        exit(0);                                                                /* Buh bye */
}


if (ioperm(LPT1PORT, 3, 1)) {perror("ioperm"); exit(1);}                        /* Get access to the ports */

ioctl(0,TCGETS,&oldT);                                                                /* get current terminal mode */
newT=oldT;                                                                        /* copy it into our new structure */
newT.c_lflag &= ~ECHO;                                                                /* toggle echo */
newT.c_lflag &= ~ICANON;                                                        /* toggle no line delimination */
ioctl(0,TCSETS,`"wT);                                                                /* initialize our new terminal mode */
char i[1];

while(1==1){

        read(0,&i[0],1);                                                        /* read one char from the buffer */
        printf("\nu pressed %c\n", i[0]);
        if (i[0] == '8') { forward(i[0]); }
        if (i[0] == '2') { reverse(i[0]); }
        if (i[0] == '4') { left(i[0]); }
        if (i[0] == '6') { right(i[0]); }
        if (i[0] == '7') { forwardleft(i[0]); }
        if (i[0] == '9') { forwardright(i[0]); }
        if (i[0] == '1') { reverseleft(i[0]); }
        if (i[0] == '3') { reverseright(i[0]); }
        if (i[0] == '5') { stop(i[0]); }
        if (i[0] == 'q') { end(); }

}

}

/* end of rccar.c */


Here are my errors.

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
[sudo] password for shawn:
rccar.c:47:20: error: asm/io.h: No such file or directory
rccar.c: In function ‘main’:
rccar.c:124: error: stray ‘`’ in program
rccar.c:124:17: warning: missing terminating " character
rccar.c:124: error: missing terminating " character
rccar.c:125: error: expected expression before ‘char’
rccar.c:144: error: expected ‘;’ before ‘}’ token


Thanks for any help you're able to provide.

Shawn

smeezekitty 10-17-2009 12:24 AM

change #include <asm/io.h> to #include <io.h>

Danteleo 10-17-2009 12:29 AM

changed #include <asm/io.h> to #include <io.h> and now get these errors.

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
[sudo] password for shawn:
rccar.c:47:16: error: io.h: No such file or directory
rccar.c: In function ?main?:
rccar.c:124: error: stray ?`? in program
rccar.c:124:17: warning: missing terminating " character
rccar.c:124: error: missing terminating " character
rccar.c:125: error: expected expression before ?char?
rccar.c:144: error: expected ?;? before ?}? token

Thank You for your help.

Shawn

btncix 10-17-2009 12:35 AM

Code:

rccar.c:124: error: stray ‘`’ in program
rccar.c:124:17: warning: missing terminating " character
rccar.c:124: error: missing terminating " character
rccar.c:125: error: expected expression before ‘char’

change
ioctl(0,TCSETS,`"wT); to ioctl(0,TCSETS,"wT");



The <asm/io.h> error seems to be related to whether you have the kernel source package installed.

see http://www.linuxquestions.org/questi...smio.h-595596/
see http://sources.redhat.com/ml/cygwin/.../msg01301.html

smeezekitty 10-17-2009 12:39 AM

just comment out #include <io.h>
and do the suggested fix from btncix

xeleema 10-17-2009 12:43 AM

Check your "Include" Paths
 
Greetingz!

I'm assuming there's just a "Makefile" file in the source directory, and there's no autoconf "config" script.

If that's the case, you may need to modify the Makefile. However, you first have to knock that io.h problem out of the park.

1. Run the "updatedb" command as root (use "sudo updatedb" if you can).
This command should not return any output, but just give you back your shell prompt when it's done.

2. Run "locate io.h" and take note of every path the "io.h" file is found.

3. Make a quick backup of your "Makefile" ("cp -p Makefile Makefile.bak").

4. Check your "Makefile" for any "-I /path/to/some/directory" statements, correct any non-existing directories.
(NOTE: when you add a path, don't put "io.h" at the end of it.)

If that still doesn't work, let us know! (and be sure to mention what changes you made to the Makefile.

UPDATE: I just pulled down the source. No "Makefile", it's just a .c script. Nevermind! :)

smeezekitty 10-17-2009 12:47 AM

Quote:

Originally Posted by xeleema (Post 3722408)
Greetingz!

I'm assuming there's just a "Makefile" file in the source directory, and there's no autoconf "config" script.

If that's the case, you may need to modify the Makefile. However, you first have to knock that io.h problem out of the park.

1. Run the "updatedb" command as root (use "sudo updatedb" if you can).
This command should not return any output, but just give you back your shell prompt when it's done.

2. Run "locate io.h" and take note of every path the "io.h" file is found.

3. Make a quick backup of your "Makefile" ("cp -p Makefile Makefile.bak").

4. Check your "Makefile" for any "-I /path/to/some/directory" statements, correct any non-existing directories.
(NOTE: when you add a path, don't put "io.h" at the end of it.)

If that still doesn't work, let us know! (and be sure to mention what changes you made to the Makefile.

UPDATE: I just pulled down the source. No "Makefile", it's just a .c script. Nevermind! :)

there is no makefile and io.h should not be required on linux

Danteleo 10-17-2009 12:52 AM

Thanks for all the help.




To make sure I had the Headers I did this.

Code:

shawn@shawn-desktop:~/Desktop$ locate 'asm/io.h'
/usr/src/linux-headers-2.6.28-15/arch/alpha/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/arm/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/avr32/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/blackfin/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/cris/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/h8300/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/ia64/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/m68knommu/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/mips/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/parisc/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/powerpc/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/s390/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/sh/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/sparc/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/um/include/asm/io.h
/usr/src/linux-headers-2.6.28-15/arch/x86/include/asm/io.h

I commented out #include <io.h> and fixed the missing quote and got this.

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
rccar.c: In function ‘main’:
rccar.c:124: error: stray ‘`’ in program
rccar.c:129: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result

Thx

xeleema 10-17-2009 01:04 AM

I get something different...strange.
 
smeezekitty,
I noticed the lack of a Makefile. Updated my post to reflect that almost right after I chased down the sourcecode myself :)

Danteleo,

I'm getting a different error, and I've made the changes that everyone's mentioned. However, I'm not sure why the author put "must do as root", typically you should be able to compile things as a regular user....

xeleema 10-17-2009 01:08 AM

Different Errors for Me
 
I'm getting different errors myself. I've commented-out the Include for asm/io.h and corrected the ioctl line.

Code:

sysop$ gcc -O2 -o rccar rccar.c
rccar.c: In function 'end':
rccar.c:111: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c:113: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c: In function 'main':
rccar.c:117: warning: incompatible implicit declaration of built-in function 'exit'
/tmp/cccwFwJZ.o: In function `main':
rccar.c:(.text+0xd9): undefined reference to `outb'
rccar.c:(.text+0x10b): undefined reference to `outb'
rccar.c:(.text+0x13b): undefined reference to `outb'
rccar.c:(.text+0x16e): undefined reference to `outb'
rccar.c:(.text+0x19b): undefined reference to `outb'
/tmp/cccwFwJZ.o:rccar.c:(.text+0x1ce): more undefined references to `outb' follow
collect2: ld returned 1 exit status
sysop$

I also get the same errors when I try it as root;


Code:

root# gcc -O2 -o rccar rccar.c
rccar.c: In function 'end':
rccar.c:111: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c:113: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c: In function 'main':
rccar.c:117: warning: incompatible implicit declaration of built-in function 'exit'
/tmp/ccIVzpBr.o: In function `main':
rccar.c:(.text+0xd9): undefined reference to `outb'
rccar.c:(.text+0x10b): undefined reference to `outb'
rccar.c:(.text+0x13b): undefined reference to `outb'
rccar.c:(.text+0x16e): undefined reference to `outb'
rccar.c:(.text+0x19b): undefined reference to `outb'
/tmp/ccIVzpBr.o:rccar.c:(.text+0x1ce): more undefined references to `outb' follow
collect2: ld returned 1 exit status


Danteleo 10-17-2009 01:11 AM

Here is the current version (edited) that I am using.

Code:

/*
* rccar.c: lpt port control of a radio controlled car
*
* MORE INFO HERE:      http://www.engr.uvic.ca/~sbowman/prog.php
*
* By:            Shaun Bowman Sept. 2003, shaun_bowman@hotmail.com
*
* Notes: This program is unlikely to be ready-2-port because of the ioctl
method
*          of changing terminal settings. This program was written so i could
*          control a tiny 10$ remote control car (and to procrastinate homework).
*          The lpt port pins (6,7,8,9) are used to control Forward, Reverse, Left
*          and Right in the controller. In addition to this program, I had to build
*          a simple circuit to bypass the buttons on the remote control. This is
*          because even in the low state, the Control and Ground pins still form a
*          complete circuit.
*
*          Crude Diagram (1 for each button):
*
*          GrndPin(18-25)  PinX(5-9)
*                |                |                          T1: NPN transistor, i used 2N222
*                |                |
*                |                L---R1--R2--R3                R1-R3: Resistors, i didn't have
*                |        T1:                    |                      ones with high enough
*                |--------|\                    |                resistance, so i had to use
*                |        | ------------------                  3. Total R ~= 100k ohm's
*              |    |---|/(<-collector pin)
*                |    |
*      -ButtnLead  +ButtnLead
*
* Compile with: gcc -O2 -o rccar rccar.c (must be root)
*
* Pin bits (numbered at female end)
*  outb(8, x)  = pin 5      UNUSED
*  outb(16, x)  = pin 6      Forward
*  outb(32, x)  = pin 7      Reverse
*  outb(64, x)  = pin 8      Left
*  outb(128, x) = pin 9      Right
*  outb(80, x)  = pin 6 & 8  FORWARDLEFT
*  outb(144, x) = pin 6 & 9  FORWARDRIGHT
*  outb(96, x)  = pin 7 & 8  REVERSELEFT
*  outb(160, x) = pin 7 & 9  REVERSERIGHT
*/

#include <stdio.h>
#include <unistd.h>
/* #include <io.h> */
#include <string.h>
#include <libio.h>
#include <stdlib.h>

#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

#define LPT1PORT 0x378                                                                /* lpt1 */

int main()
{

struct termios oldT, newT;                                                        /* create our termios identifiers, uphere because C++ is retarted*/

void forward( char key ){
        outb(16, LPT1PORT);                                                        /* Set pin 9 to high */
        usleep(10000);                                                                /* Sleep for a while (100 ms) */
}

void reverse( char key ){
        outb(32, LPT1PORT);
        usleep(10000);
}

void left( char key ){
        outb(64, LPT1PORT);
        usleep(10000);
}

void right( char key ){
        outb(128, LPT1PORT);
        usleep(10000);
}

void forwardleft( char key ){
        outb(80, LPT1PORT);
        usleep(10000);
}

void forwardright( char key ){
        outb(144, LPT1PORT);
        usleep(10000);
}

void reverseleft( char key ){
        outb(96, LPT1PORT);
        usleep(10000);
}

void reverseright( char key ){
        outb(160, LPT1PORT);
        usleep(10000);
}


void stop( char key ){
        outb(0, LPT1PORT);                                                        /* Set the data signals (D0-7) of the port to all low (0) */
        usleep(10000);
}

void end () {                                                                        /* The Beggining of the end.... */
        outb(0, LPT1PORT);
        usleep(10000);
        if (ioperm(LPT1PORT, 3, 0)) {perror("ioperm"); exit(1);}                /* We don't need the port anymore */
        ioctl(0,TCSETS,&oldT);                                                        /* Better set the terminal back to normal... :-) */
        exit(0);                                                                /* Buh bye */
}


if (ioperm(LPT1PORT, 3, 1)) {perror("ioperm"); exit(1);}                        /* Get access to the ports */

ioctl(0,TCGETS,&oldT);                                                                /* get current terminal mode */
newT=oldT;                                                                        /* copy it into our new structure */
newT.c_lflag &= ~ECHO;                                                                /* toggle echo */
newT.c_lflag &= ~ICANON;                                                        /* toggle no line delimination */
ioctl(0,TCSETS,`"wT");                                                                /* initialize our new terminal mode */
char i[1];

while(1==1){

        read(0,&i[0],1);                                                        /* read one char from the buffer */
        printf("\nu pressed %c\n", i[0]);
        if (i[0] == '8') { forward(i[0]); }
        if (i[0] == '2') { reverse(i[0]); }
        if (i[0] == '4') { left(i[0]); }
        if (i[0] == '6') { right(i[0]); }
        if (i[0] == '7') { forwardleft(i[0]); }
        if (i[0] == '9') { forwardright(i[0]); }
        if (i[0] == '1') { reverseleft(i[0]); }
        if (i[0] == '3') { reverseright(i[0]); }
        if (i[0] == '5') { stop(i[0]); }
        if (i[0] == 'q') { end(); }

}

}

/* end of rccar.c */


and here are the remaining errors that I am getting.

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
rccar.c: In function ‘main’:
rccar.c:124: error: stray ‘`’ in program
rccar.c:129: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result

Not sure on why being root either, figured it was just in case.

Danteleo 10-17-2009 01:13 AM

Make sure you add

#include <stdlib.h>


that fixed a couple of problems I had earlier.

smeezekitty 10-17-2009 01:14 AM

Quote:

Originally Posted by xeleema (Post 3722424)
I'm getting different errors myself. I've commented-out the Include for asm/io.h and corrected the ioctl line.

Code:

sysop$ gcc -O2 -o rccar rccar.c
rccar.c: In function 'end':
rccar.c:111: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c:113: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c: In function 'main':
rccar.c:117: warning: incompatible implicit declaration of built-in function 'exit'
/tmp/cccwFwJZ.o: In function `main':
rccar.c:(.text+0xd9): undefined reference to `outb'
rccar.c:(.text+0x10b): undefined reference to `outb'
rccar.c:(.text+0x13b): undefined reference to `outb'
rccar.c:(.text+0x16e): undefined reference to `outb'
rccar.c:(.text+0x19b): undefined reference to `outb'
/tmp/cccwFwJZ.o:rccar.c:(.text+0x1ce): more undefined references to `outb' follow
collect2: ld returned 1 exit status
sysop$

I also get the same errors when I try it as root;


Code:

root# gcc -O2 -o rccar rccar.c
rccar.c: In function 'end':
rccar.c:111: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c:113: warning: incompatible implicit declaration of built-in function 'exit'
rccar.c: In function 'main':
rccar.c:117: warning: incompatible implicit declaration of built-in function 'exit'
/tmp/ccIVzpBr.o: In function `main':
rccar.c:(.text+0xd9): undefined reference to `outb'
rccar.c:(.text+0x10b): undefined reference to `outb'
rccar.c:(.text+0x13b): undefined reference to `outb'
rccar.c:(.text+0x16e): undefined reference to `outb'
rccar.c:(.text+0x19b): undefined reference to `outb'
/tmp/ccIVzpBr.o:rccar.c:(.text+0x1ce): more undefined references to `outb' follow
collect2: ld returned 1 exit status


thats odd i thought outb is a standard library function
@Danteleo
you must not have noticed there
is a junk ` symbol that needs to be removed
ioctl(0,TCSETS,`"wT"); the bold red part needs to be removed

smeezekitty 10-17-2009 01:17 AM

try changing ioctl(0,TCSETS,`"wT"); to ioctl(0,TCSETS,"wT");
notice the '`' not there

Danteleo 10-17-2009 01:17 AM

Missed that one...thx

Now my new list of error...lol

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
[sudo] password for shawn:
rccar.c: In function ‘main’:
rccar.c:129: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result
/tmp/cc6lYHO9.o: In function `main':
rccar.c:(.text+0x11e): undefined reference to `outb'
rccar.c:(.text+0x150): undefined reference to `outb'
rccar.c:(.text+0x180): undefined reference to `outb'
rccar.c:(.text+0x1b0): undefined reference to `outb'
rccar.c:(.text+0x1e0): undefined reference to `outb'
/tmp/cc6lYHO9.o:rccar.c:(.text+0x210): more undefined references to `outb' follow
collect2: ld returned 1 exit status



All times are GMT -5. The time now is 04:25 PM.