using a shared lookup table
Im having some problems setting up and using a shared lookup table between multiple files.
At first I thought my problems were just because I forgot about using externs, now Im really not sure how you should go about it. I don't want to redefine it (with the values)in every file and you cant assign the values to it when you declare it. For my example the table is small but in my program its kind of big and messy.
So to keep things simple, lets say you have main.c
Code:
#include "stuff.h"
int main() {
foo(2);
return 0;
}
stuff.h
Code:
#ifndef _STUFF_H_
#define _STUFF_H_
int foo(int y);
#endif
stuff.c
Code:
#include "data.h"
int foo(int y) {
return (2 * lookup_table[y]);
}
data.h
Code:
#ifndef _DATA_H_
#define _DATA_H_
int lookup_table[4] = {42, 43, 44, 45};
int lookup(int x);
#endif
and data.c
Code:
#include "data.h"
int lookup(int x) {
return lookup_table[x];
}
With this example I get these errors
Quote:
/usr/bin/ld: multiple definitions of symbol _lookup_table
/var/tmp//ccu9bk8L.o definition of _lookup_table in section (__DATA,__data)
/var/tmp//cc2RkuxQ.o definition of _lookup_table in section (__DATA,__data)
collect2: ld returned 1 exit status
So how should you go about this?
Thanks