简体   繁体   English

XC8 编译器是否支持弱符号?

[英]Does XC8 compiler support weak symbols?

gcc has __attribute__((weak)) which allows to create a weak symbol such as a function. gcc 有__attribute__((weak))允许创建弱符号,例如函数。 This allows the user to redefine a function.这允许用户重新定义函数。 I would like to have the same behavior in XC8.我想在 XC8 中具有相同的行为。

More info:更多信息:

I am writing a driver for XC8 and I would like to delegate low level initialization to a user defined function.我正在为 XC8 编写驱动程序,我想将低级初始化委托给用户定义的函数。

I know it is possible to redefine a function: there is the putch function that is implemented in XC8's source file and which is called by the printf function.我知道可以重新定义一个函数:在 XC8 的源文件中实现了putch函数,它由 printf 函数调用。 The user is allowed to reimplement putch inside his application.允许用户在其应用程序中重新实现putch There are two functions with the same name, but no error is raised.有两个同名的函数,但没有引发错误。

putch 's implementation in XC8's source files has a comment saying "Weak implementation. User implementation may be required", so it must be possible. putch在 XC8 的源文件中的实现有一条评论说“弱实现。可能需要用户实现”,所以它必须是可能的。

I looked at pragmas in XC8's user guide, but there is no directive related to this question.我查看了 XC8 用户指南中的编译指示,但没有与此问题相关的指令。

A linker will only search static libraries to resolve a symbol that is not already resolved by input object files, so replacing static library functions can be done without weak linkage.链接器只会搜索静态库来解析输入目标文件尚未解析的符号,因此可以在没有弱链接的情况下替换静态库函数。 Weak linkage is useful for code provided as source or object code rather then as a static library.弱链接对于作为源代码或目标代码而不是作为静态库提供的代码很有用。

So if no weak linkage directive is supported, you could create a static library for the "weak" symbols and link that.因此,如果不支持弱链接指令,您可以为“弱”符号创建一个静态库并链接它。

The XC8 manual documents behaviour for both the IAR compatibility directive __weak and a weak pragma, and in both cases the directives are ignored (supported only in XC16 and XC32), so you will have to use the above suggested method, which is in any case far more portable - if somewhat inconvenient. XC8 手册记录了 IAR 兼容性指令__weakweak编译指示的行为,在这两种情况下,指令都被忽略(仅在 XC16 和 XC32 中受支持),因此您必须使用上述建议的方法,无论如何更便携 - 如果有点不方便。

In the case of putch() I suspect that this is not working as you believe.putch()的情况下,我怀疑这不像你相信的那样工作。 I would imagine that this is not a matter of weak linkage at all;我想这根本不是弱链接的问题; in the static library containing printf() an unresolved link to putch() exists, and the linker resolves it with whatever you provide;在包含printf()的静态库中,存在指向putch() printf()的未解析链接,链接器使用您提供的任何内容解析它; if you were to compile and link both the Microchip implementation and yours from source code you would get a linker error;如果您要从源代码编译和链接 Microchip 实现和您的实现,则会出现链接器错误; equally if you were to provide no implementation whatsoever you would get a linker error.同样,如果您不提供任何实现,则会出现链接器错误。

XC8 compiler does support the "weak" attribute. XC8 编译器确实支持“弱”属性。

The weak attribute causes the declaration to be emitted as a weak symbol.弱属性导致声明作为弱符号发出。 A weak symbol indicates that if a global version of the same symbol is available, that version should be used instead.弱符号表示如果同一符号的全球版本可用,则应改用该版本。 When the weak attribute is applied to a reference to an external symbol, the symbol is not required for linking.当弱属性应用于对外部符号的引用时,链接不需要该符号。

For example:例如:

extern int __attribute__((weak)) s;

int foo(void) 
{
    if (&s)
        return s;
    return 0;   /* possibly some other value */
}

In the above program, if s is not defined by some other module, the program will still link but s will not be given an address.在上面的程序中,如果 s 没有被其他模块定义,程序仍然会链接,但不会给 s 一个地址。 The conditional verifies that s has been defined (and returns its value if it has).条件验证 s 是否已定义(如果已定义,则返回其值)。 Otherwise '0' is returned.否则返回“0”。 There are many uses for this feature, mostly to provide generic code that can link with an optional library.此功能有很多用途,主要是提供可以与可选库链接的通用代码。

A variable can also be qualified with the "weak" attribute.变量也可以使用“weak”属性进行限定。

For example:例如:

char __attribute__((weak)) input;
char input __attribute__((weak));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM