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/)

smeezekitty 10-17-2009 01:22 AM

see here

Danteleo 10-17-2009 01:29 AM

Isn't that why it uses this command to run the script?

sudo gcc -O2 -o rccar rccar.c


I don't know C/C++ so it's just a guess.

smeezekitty 10-17-2009 01:31 AM

u got me there this time

xeleema 10-17-2009 01:34 AM

"man outb" for me...
 
Hm,
According to the man page for my distribution ("man outb")

Quote:

The value argument is passed first and the port argument is passed second
And from what I can tell, that's exactly what rccar.c is doing;

Code:

sysop$ nl rccar.c|grep outb
    34  * *  outb(8, x)  = pin 5      UNUSED
    35  * *  outb(16, x)  = pin 6      Forward
    36  * *  outb(32, x)  = pin 7      Reverse
    37  * *  outb(64, x)  = pin 8      Left
    38  * *  outb(128, x) = pin 9      Right
    39  * *  outb(80, x)  = pin 6 & 8  FORWARDLEFT
    40  * *  outb(144, x) = pin 6 & 9  FORWARDRIGHT
    41  * *  outb(96, x)  = pin 7 & 8  REVERSELEFT
    42  * *  outb(160, x) = pin 7 & 9  REVERSERIGHT
    64  outb(16, LPT1PORT);    /* Set pin 9 to high */
    69  outb(32, LPT1PORT);
    74  outb(64, LPT1PORT);
    79  outb(128, LPT1PORT);
    84  outb(80, LPT1PORT);
    89  outb(144, LPT1PORT);
    94  outb(96, LPT1PORT);
    99  outb(160, LPT1PORT);
  105  outb(0, LPT1PORT);      /* Set the data signals (D0-7) of the port to all low (0) */
  110  outb(0, LPT1PORT);

However we have 9 "outb(#, x)" where I'm thinking we should be defining the port (LPT1PORT), rather than just "x"

I'm thinking that maybe one of the "outb" friends might help....

As for the optimization flags ("-O" versus "-O2" versus nothing), I think we're safe. The "outb" doesn't appear to be affected by those flags....

smeezekitty 10-17-2009 01:39 AM

Quote:

Originally Posted by xeleema (Post 3722450)
Hm,
As for the optimization flags ("-O" versus "-O2" versus nothing), I think we're safe. The "outb" doesn't appear to be affected by those flags....

did you read my link?
@Original poster
try putting this on the top of the file
#define outb(n, port) outportb(port, n)
#define inpb inportb

xeleema 10-17-2009 01:39 AM

Found more "outb" information....
 
Found an interesting chunk over here;

Quote:

Obsolete IO macros

Back in a time when AVR-GCC and avr-libc could not handle IO port access in the direct assignment form as they are handled now, all IO port access had to be done through specific macros that eventually resulted in inline assembly instructions performing the desired action.

These macros became obsolete, as reading and writing IO ports can be done by simply using the IO port name in an expression, and all bit manipulation (including those on IO ports) can be done using generic C bit manipulation operators.

The macros in this group simulate the historical behaviour. While they are supposed to be applied to IO ports, the emulation actually uses standard C methods, so they could be applied to arbitrary memory locations as well.
#define inp(port) (port)
#define outp(val, port) (port) = (val)
#define inb(port) (port)
#define outb(port, val) (port) = (val)
#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (bit))


smeezekitty 10-17-2009 01:43 AM

Quote:

Originally Posted by xeleema (Post 3722454)
Found an interesting chunk over here;

that obviously will not work

Danteleo 10-17-2009 01:45 AM

Got this after adding
#define outb(n, port) outportb(port, n)
#define inpb inport



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

smeezekitty 10-17-2009 01:46 AM

dang it!
this is one of those few reasons dos/windows is better then linux

xeleema 10-17-2009 01:47 AM

FYI: swapping the port and value for all the "outb" statements didn't work.

Code:

sysop$ diff rccar.c rccar.c.new
64c64
< outb(LPT1PORT, 16);    /* Set pin 9 to high */
---
> outb(16, LPT1PORT);    /* Set pin 9 to high */
69c69
< outb(LPT1PORT, 32);
---
> outb(32, LPT1PORT);
74c74
< outb(LPT1PORT, 64);
---
> outb(64, LPT1PORT);
79c79
< outb(LPT1PORT, 128);
---
> outb(128, LPT1PORT);
84c84
< outb(LPT1PORT, 80);
---
> outb(80, LPT1PORT);
89c89
< outb(1LPT1PORT, 44);
---
> outb(144, LPT1PORT);
94c94
< outb(LPT1PORT, 96);
---
> outb(96, LPT1PORT);
99c99
< outb(LPT1PORT, 160);
---
> outb(160, LPT1PORT);
105c105
< outb(LPT1PORT, 0);      /* Set the data signals (D0-7) of the port to all low (0) */
---
> outb(0, LPT1PORT);      /* Set the data signals (D0-7) of the port to all low (0) */


xeleema 10-17-2009 01:54 AM

"Teh Win" or "Fail" ?
 
Okay, get this, I added "#include <sys/io.h>

Code:

sysop$ gcc -O2 -o rccar rccar.c
sysop$ ldd rccar
        linux-gate.so.1 =>  (0xffffe000)
        libc.so.6 => /lib/libc.so.6 (0xb7f0a000)
        /lib/ld-linux.so.2 (0xb806b000)
sysop$ ./rccar
ioperm: Operation not permitted

*SO* close! I think we need to call i386_set_ioperm instead of just ioperm....

smeezekitty 10-17-2009 01:56 AM

try compiling with gcc -O2 rccar rccar.c

Danteleo 10-17-2009 01:59 AM

When I added #include <sys/io.h>

I got this

Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 -o rccar rccar.c
[sudo] password for shawn:
rccar.c:46:1: warning: "inpb" redefined
rccar.c:45:1: warning: this is the location of the previous definition
rccar.c: In function ?main?:
rccar.c:134: warning: ignoring return value of ?read?, declared with attribute warn_unused_result


Danteleo 10-17-2009 02:02 AM

I get this when I compile using


sudo gcc -O2 rccar rccar.c

as opposed to

sudo gcc -O2 -o rccar rccar.c


Code:

shawn@shawn-desktop:~/Desktop$ sudo gcc -O2 rccar rccar.c                                   
rccar.c:46:1: warning: "inpb" redefined                                                     
rccar.c:45:1: warning: this is the location of the previous definition
rccar.c: In function ‘main’:
rccar.c:134: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result
rccar: In function `_start':
/build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:/build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:65: first defined here
rccar:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here
rccar: In function `_fini':
/build/buildd/glibc-2.9/csu/../sysdeps/generic/initfini.c:109: multiple definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crti.o:/build/buildd/glibc-2.9/csu/../sysdeps/generic/initfini.c:109: first defined here
rccar:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
rccar: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o:(.data+0x0): first defined here
rccar: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.3.3/crtbegin.o:(.data+0x0): first defined here
rccar: In function `_init':
/build/buildd/glibc-2.9/build-tree/i386-libc/csu/crti.S:15: multiple definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crti.o:/build/buildd/glibc-2.9/build-tree/i386-libc/csu/crti.S:15: first defined here
/tmp/cciBw3vh.o: In function `main':
rccar.c:(.text+0x0): multiple definition of `main'
rccar:(.text+0xc0): first defined here
/usr/lib/gcc/i486-linux-gnu/4.3.3/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
rccar:(.dtors+0x4): first defined here
collect2: ld returned 1 exit status


xeleema 10-17-2009 02:07 AM

Quote:

try compiling with gcc -O2 rccar rccar.c
Dropping the "-o" doesn't even work for me, I get

Quote:

root# gcc -O2 rccar rccar.c
gcc: rccar: No such file or directory


All times are GMT -5. The time now is 06:27 AM.