-2

I am slightly confused about using "extern" in my c code, when there is a global variable involved. I tried the following, and got a compilation error:

Main.c:

extern unsigned short *videobuffer;
//I also tried this in a separate and it failed with the same compilation error//
extern (unsigned short *)videobuffer;

lib.c:

unsigned short *videobuffer = (unsigned short *)0x6000000;

The error I received:

[COMPILE] Compiling main.c
main.c:16: error: expected identifier or '(' before 'unsigned'
make: *** [main.o] Error 1
Shanty
  • 101
  • 8
  • How do you compile and link these two sources? You can *compile* but not *link* just your `main.c`. (And better don't try random stuff such as throwing in parentheses "until it works". You can easily look up the correct syntax elsewhere.) – Jongware Oct 24 '15 at 23:13

2 Answers2

0

Valid code is usually what people want here:

main.c

#include <stdio.h>
extern unsigned short *videobuffer;

int main() {
    printf("%p\n", videobuffer);
}

lib.c

unsigned short *videobuffer = (unsigned short *)0x6000000;
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

the following is correct:

Main.c:

extern unsigned short *videobuffer;

lib.c:

unsigned short *videobuffer = (unsigned short *)0x6000000;

The extern keyword tells the compiler there is a variable, but it is not in this compilation unit.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • I tried this, but I am still getting the same error in compilation as stated above. – Shanty Oct 24 '15 at 22:22
  • Then you declare it at the wrong place. It must be before any statement and would best be declared before any function, after all includes. There can also be a syntax error before the declaration, which only triggers a compiler error later. Please show us more code. – Paul Ogilvie Oct 24 '15 at 22:41