Page 1 of 1

macro expansion expected but failed by gcc

Posted: Wed Jun 03, 2020 3:42 am
by ITchimp
My header file (low_level.h)

Code: Select all

#ifdef LOW_LEVEL_H
#define LOW_LEVEL
#define inb(p) port_byte_in(p)
#define outb(p, b) port_byte_out(p,b)

unsigned char port_byte_in(unsigned short port)
void port_byte_out(unsigned short port, unsigned char data)
#endif
my library implementation source file (low_level.c)

Code: Select all

#include "low_level.h"
unsigned char port_byte_in(unsigned short port){
   unsigned char result;
   asm(" in %%dx, %%al":"=a"(result):"d" (port));
   return result;
}
void port_byte_out(unsigned short port, unsigned char data){
   asm("out %%al, (%%dx)"::"a"(data), "d"(port));
}
my main.c

Code: Select all

#include "low_level.h"
#include <stdio.h>

void main(){
      port_byte_out(0x10, 14);
      outb(0x10,14);
}
compile with gcc gives me linking error indicating symbol outb not found.. but isn't it a macro
which gcc should expand?

Re: macro expansion expected but failed by gcc

Posted: Wed Jun 03, 2020 4:29 am
by iansjack
Well....

You've wrapped your header file in

Code: Select all

#ifdef LOW_LEVEL_H
...
#endif
LOW_LEVEL_H isn't defined, so the header never gets included. You'll also note the mistake in the second line of your header.

This explains your initial problem, but fixing it then throws up a further one. I think it's a useful exercise for you to try to solve that one yourself.