如何使弱链接的工作与海湾合作委员会? [英] How to make weak linking work with GCC?

查看:114
本文介绍了如何使弱链接的工作与海湾合作委员会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有似乎是告诉GCC以薄弱环节的象征的3种方式:

There seem to be 3 ways of telling GCC to weak link a symbol:


  • __ __属性((weak_import))

  • __ __属性((弱))

  • 的#pragma弱SYMBOL_NAME

  • __attribute__((weak_import))
  • __attribute__((weak))
  • #pragma weak symbol_name

这些工作都没有给我:

#pragma weak asdf
extern void asdf(void) __attribute__((weak_import, weak));
...
{
    if(asdf != NULL) asdf();
}

我总是得到一个链接错误是这样的:

I always get a link error like this:

Undefined symbols:
  "_asdf", referenced from:
      _asdf$non_lazy_ptr in ccFA05kN.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我在OS X 10.5.5使用GCC 4.0.1。我在做什么错了?

I am using GCC 4.0.1 on OS X 10.5.5. What am I doing wrong?

推荐答案

我只是看着这个,觉得有些人可能有兴趣在我的调查结果。

I just looked into this and thought some others might be interested in my findings.

与weak_import弱联真的只用动态库效果很好。你可以得到它(通过指定-undefined dynamic_lookup按照以上建议)与静态链接的工作,但是这并不是如此火爆的想法。这意味着没有未定义的符号将直到运行时被检测到。这是我会避免在生产中code,个人。

Weak linking with weak_import really only works well with dynamic libraries. You can get it to work with static linking (by specifying -undefined dynamic_lookup as suggested above) but this isn't such a hot idea. It means that no undefined symbols will be detected until runtime. This is something I would avoid in production code, personally.

下面是展示如何使其工作在Mac OS X终端会话:

Here is a Mac OS X Terminal session showing how to make it work:

下面是F.C

int f(int n)
{
    return n * 7;
}

下面是whatnof.c

Here is whatnof.c

#include <stdio.h>
#include <stdlib.h>

extern int f (int) __attribute__((weak_import));

int main() {
    if(f == NULL)
        printf("what, no f?\n");
    else
        printf("f(8) is %d\n", f(8));
    exit(0);
}

从F.C做一个动态库:

Make a dynamic library from f.c:

$ cc -dynamiclib -o f.dylib f.c

编译和对动态链接的lib,列出动态库。

Compile and link against the dynamic lib, list dynamic libs.

$ cc -o whatnof whatnof.c f.dylib
$ otool -L whatnof
whatnof:
       f.dylib (compatibility version 0.0.0, current version 0.0.0)
       /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

运行whatnof看看会发生什么:

Run whatnof to see what happens:

$ whatnof
f(8) is 56

现在用空库替换f.dylib(无符号):

Now replace f.dylib with an empty library (no symbols):

$ mv f.dylib f.dylib.real
$ touch null.c
$ cc -dynamiclib -o f.dylib null.c

运行相同whatnof看看会发生什么:

Run same whatnof to see what happens:

$ whatnof
what, no f?

有关weak_import的基本思想(或用例)是它可以让你对一组动态(共享)库链接,但然后对早期版本相同的库相同code。您可以检查功能对NULL,看看他们是否在该code目前正在运行针对特定动态库会支持。这似乎是用X code支持的基本发展模式的一部分。我希望这个例子是非常有用的;它帮助把我的心踏实关于X code设计的一部分。

The basic idea (or "use case") for weak_import is that it lets you link against a set of dynamic (shared) libraries, but then run the same code against earlier versions of the same libraries. You can check functions against NULL to see if they're supported in the particular dynamic library that the code is currently running against. This seems to be part of the basic development model supported by Xcode. I hope this example is useful; it has helped put my mind at ease about this part of the Xcode design.

这篇关于如何使弱链接的工作与海湾合作委员会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆