Skip to content

Instantly share code, notes, and snippets.

@afcidk
Created March 12, 2019 15:45
Show Gist options
  • Save afcidk/fe227edd29bb6a3674e9d778fc171840 to your computer and use it in GitHub Desktop.
Save afcidk/fe227edd29bb6a3674e9d778fc171840 to your computer and use it in GitHub Desktop.
Sample code of gnu c extension alias
// Reference: https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
#include <stdio.h>
extern void func(void);
void print_hi(void) {
printf("hello~\n");
}
void func(void) __attribute__((alias("print_hi")));
int main() {
func();
}
/*
* 可以發現,我們沒有實作 func 這個函式,但是在 main 裏面卻可以呼叫 func()。
* 這是因為我們在第 11 行把 func() 和 print_hi() 關聯起來了,所以呼叫 func() 就等於呼叫 print_hi()
* 試著把第 11 行註解掉,可以發現編譯沒辦法通過,因為這樣我們沒有定義到 func() 的行為。
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment