开发者

Is there a way to make a function global to the library and to those who include/link the library?

开发者 https://www.devze.com 2023-01-04 22:17 出处:网络
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 s开发者_StackOverflow社区eem so... What I want right now, is to have s

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 s开发者_StackOverflow社区eem 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消