有没有办法使函数对库和包含/链接库的人来说是全局的?

发布于 2024-09-07 15:08:38 字数 125 浏览 4 评论 0原文

我现在有点困惑。我认为当你在一个函数上使用 extern 时,它会成为所有东西的全局,但似乎并非如此......我现在想要的是拥有一些可以在我的静态库中使用的函数,并且在链接它的程序中。我该怎么办呢? 我正在使用 Objective-C

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2024-09-14 15:08:38

如果我在定义函数时只使用 extern 而不是 extern inline ,它对我有用。

示例: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);
}

创建的库:
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;
}

编译为:gcc -ObjC -o caller caller.m lib.a -lobjc
运行: ./caller

返回:

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

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
影子的影子 2024-09-14 15:08:38

在 CardDefs.h 上,我有:

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;
}

我必须在库内部和外部使用这个函数。我还有其他类似的功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文