macro expansion expected but failed by gcc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

macro expansion expected but failed by gcc

Post 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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: macro expansion expected but failed by gcc

Post 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.
Post Reply