Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern variables in static library, using Objective-C

I've built a static library, to be linked in my iPhone apps. This library uses some global variables and functions, like in C. My problem is, when using for example:

extern
void do_stuff (const int a)
{
    return a*a;
}

extern const int a_variable;
extern const int an_array[DEFINED_VALUE];

When I use this function, or access these variables, anywhere in my code, the compiler tells me

"_do_stuff" referenced from: -[Object testMethod] in tests.o

"_a_variable" referenced from: -[Object testMethod] in tests.o

"_an_array" referenced from: -[Object testMethod] in tests.o

Symbol(s) not found Collect2: Id returned 1 exit status

Has anyone ever faced this problem before? I know I'm doing something stupid, I'm missing some key Objective-C or C concept, but I can't really see what. So I was hoping someone could help me. Thanks in advance.

like image 724
Ricardo Ferreira Avatar asked Jun 25 '10 11:06

Ricardo Ferreira


1 Answers

These are linker errors, telling you that the referenced entities can't be found. Probably this means that you haven't added your library to the project.

As an aside, you probably should distinguish between the place where you declare these things, where they should indeed be declared as extern, and the place where you define them, where they shouldn't be. That is, you might have a header file that includes:

extern void do_stuff (const int a);
extern const int a_variable;
extern const int an_array[];

And then an implementation file that has something like:

void do_stuff (const int a)
{
    return a*a;
}

const int a_variable = 42;
const int an_array[DEFINED_VALUE] = { 1, 2, 3, 4 };

As another aside, calling something a_variable when it's actually a const is a bit misleading!

like image 92
walkytalky Avatar answered Sep 19 '22 01:09

walkytalky