views:

55

answers:

2

Hey all,

I'm a bit confused now. I thought that when you used extern on a function, it would become global to everything, but it doesn't seem so... What I want right now, is to have some set of functions that I can use in my static library and in the program that links it. How do I that? I'm using Objective-C

A: 

On CardDefs.h I have:

extern inline
card_config mcc (card_suit s, card_value v, card_points p)
{
    card_config ccfg;
    ccfg.m_suit = s;
    ccfg.m_value = v;
    ccfg.m_points = p;

    return ccfg;
}

And I have to use this function inside the library and outside. I have other functions that are similar to this.

Ricardo Ferreira
+1  A: 

It works for me, if I just use extern instead of extern inline when defining the function.

Example: inlib.h

extern int foo(int i);
extern int callsfoo(int i);

inlib.m:

#import "inlib.h"
#import "stdio.h"

extern int foo(int i) { printf("Foo: i = %d\n", i); }

extern int callsfoo(int i) {
    printf("Callsfoo:\n");
    foo(i);
}

Library created with:
gcc -ObjC -c inlib.m -o inlib.o
ar -q lib.a inlib.o

caller.m:

#import "inlib.h"
#import "stdio.h"

int main(int argc, char** argv) {
printf("Calling foo directly.\n");
foo(1);
printf("Calling foo via callsfoo.\n");
callsfoo(2);
return 0;
}

Compiled with: gcc -ObjC -o caller caller.m lib.a -lobjc
Run with: ./caller

Returns:

Calling foo directly.
Foo: i = 1
Calling foo via callsfoo.
Callsfoo:
Foo: i = 2
Pat Wallace