Ricardo Ferreira
Ricardo Ferreira

Reputation: 776

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.

Upvotes: 5

Views: 3615

Answers (2)

Ricardo Ferreira
Ricardo Ferreira

Reputation: 776

@walkytalky Well I ran nm on the .a filtered with grep to see if those symbols were exported.

host-006:Release-iphonesimulator <username>$ nm -g libCardLib.a | grep CP_
nm: no name list
     U _CP_BACK
     U _CP_FILE_EXTENSION_SUFFIX
     U _CP_FILE_PATH
     U _CP_SUIT_PREFIX
     U _CP_VALUE_PREFIX
00002020 D _CP_BACK
00002018 D _CP_FILE_EXTENSION_SUFFIX
0000201c D _CP_FILE_PATH
00002024 D _CP_FRONT
00002108 D _CP_SUIT_PREFIX
0000210c D _CP_VALUE_PREFIX
nm: no name list
nm: no name list
nm: no name list

So it seems that for each symbol there's an undefined copy?

Upvotes: 0

walkytalky
walkytalky

Reputation: 9543

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!

Upvotes: 5

Related Questions