Pregunta

Im trying to link to a extern function using a pointer to it. But everytime I try, I get compiler errors that the extern function is undeclared. I have little experience with external functions, and so cannot see my mistake. I have a function declared in lcd.c, which I am trying to get to from a seperate file memory.c.

I need to use function pointers as I am writing an emulator, and I have a array of memory address where each element of the array links to the hardware function called if that memory address is accessed. Basically allowing me to do:

    UINT8 WriteMem(int address, UINT8 data) {
      return memory[address](data);
    }

Heres my code im having problems with:

lcd.c

    UINT8 LCD_dataline_R(UINT8 data) {
      // Some code
      return aValue;
    }

lcd.h

    extern UINT8 LCD_dataline_R(UINT8 data);

memory.c

    #include "lcd.h"
    typedef UINT8 (*MemFunct)(UINT8 data);
    MemFunct ReadDataLine = LCD_dataline_R;
    .
    .
    UINT8 recieved = ReadDataLine(0x80);

I'm compiling the code using GCC on Linux Mint

and get the error

    Running command: make -f makefile memory.o
    gcc -c -Wall memory.c
    memory.c:3:25: error: ‘LCD_dataline_R’ undeclared here (not in a function)

Edit

Heres the makefile:

    CC=gcc
    CFLAGS=-Wall -std=c99 `sdl-config --cflags` `pkg-config --cflags gtk+-2.0`
    LIBS=`sdl-config --libs` `pkg-config --libs gtk+-2.0`

    all: psiora clean

    memory.o: memory.c psiora.c lcd.c
        $(CC) -c $(CFLAGS) $^

    clean:
        rm -rf *o psiora

Im only trying to compile memory.o at the moment so I've cut it down to just that part

¿Fue útil?

Solución

I think if you look at the preprocessed version of memory.c (gcc -E memory.c) that the declaration of LCD_dataline_R from lcd.h is missing. Most likely this is caused by some #if, #ifdef or #ifndef in lcd.h.

Otros consejos

I don't know what is wrong with your program, but here is an example of a situation that may reproduce a similar error.

foo.h:
extern void foo(int a);

foo.c:
#include "foo.h"
void foo(int a)
{    
}

bar.c:
//#include "foo.h"  --> Uncomment to remove error
typedef void(*fptr)(int a);
fptr fooptr=foo;

On compiling,

gcc -c foo.c bar.c

i get a similar error:

bar.c:3: error: 'foo' undeclared here (not in a function)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top