随便逛stackoverflow的时候,看到这么一个问题:c++ how to use gcc options --wrap=symbol in class method
--wrap=symbol是干嘛的呢?其实就是可以允许你对标准库中已经定义的函数(假设为symbol)再加一层封装,这样编译器在进行函数调用的解引用的时候,对symbol的解引用会被解析成__wrap_symbol,而如果你要使用真正的标注库里的函数,在调用symbol的时候必须要写成__real_symbol才可以 。
对symbol使用包装函数(wrapper function),任何对symbol未定义的引用(undefined reference)会被解析成__wrap_symbol,而任何对__real_symbol未定义的引用会被解析成symbol。即当一个名为symbol符号使用wrap功能时,工程中任何用到symbol符号的地方实际使用的是__wrap_symbol符号,任何用到__real_symbol的地方实际使用的是真正的symbol。
当然这里面有一些限制条件,比如说:
--wrap首先要让符号定义和符号引用在不同的文件中,否则的话,linker压根就看不到未定义的符号,见2。
另外,文中的代码A::test(),是有语法错误的(因为test又不是static函数,你怎么能A::test()这么写来调用它呢?)。。。
1. -Wl,<options> Pass comma-separated <options> on to the linker
之前使用过:-Wl,-rpath xxx
此处是这样:-Wl,--wrap=malloc -Wl,--wrap=foo
2. 想要得到1中 -Wl的用法,可以通过gcc -Wl --help 就可以得到具体的用法,并且同时还能得到 -Wa、-Wp等等