某函数既要被static函数调用,也要被外部调用,如何处理?

2019-12-20 21:47发布

如题,就是从模块化的角度,如何编写?

我想到的是再创建一个全局函数,内部直接调用那个static函数,即做一层封装。


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
Gorgon_Meducer
2019-12-22 15:36
本帖最后由 Gorgon_Meducer 于 2018-3-8 21:45 编辑
浮华一生 发表于 2018-3-8 20:57
同感, LZ 这算是编程洁癖不 哈哈


因为MDK提供了$Sub$和$$Super$$方法,对于公共函数,外界是可以强行对其进行替换的,
为了防止内部逻辑被篡改,给关键的静态函数再套一层公共函数是正确的做法——这样内部
其它静态函数就直接调用静态版本,外部其它模块就可以直接调用公共版本——即便篡改,
也是坑外不坑内。


Use of $Super$$ and $Sub$$ to patch symbol definitions
http://infocenter.arm.com/help/i ... e1362065967698.html


对于那些打不开链接的人,直接看这里:


There are special patterns you can use for situations where an existing symbol cannot be modified.

An existing symbol cannot be modified, for example, if it is located in an external library or in ROM code. In such cases you can use the $Super$$ and $Sub$$ patterns to patch an existing symbol.

To patch the definition of the function foo():
    $Super$$foo
Identifies the original unpatched function foo(). Use this to call the original function directly.
    $Sub$$foo
Identifies the new function that is called instead of the original function foo(). Use this to add processing before or after the original function.

Example
The following example shows how to use $Super$$ and $Sub$$ to insert a call to the function ExtraFunc() before the call to the legacy function foo().

  1. extern void ExtraFunc(void); extern void $Super$foo(void):
  2. /* this function is called instead of the original foo() */
  3. void $Sub$foo(void)
  4. {
  5.   ExtraFunc();    /* does some extra setup work */
  6.   $Super$foo();  /* calls the original foo() function */
  7.                   /* To avoid calling the original foo() function
  8.                    * omit the $Super$foo(); function call.
  9.                    */
  10. }
复制代码

一周热门 更多>