WPS?2019如何清理云空間?WPS 2019如何將PDF轉換為WORD?
WPS 2019如何清理云空間?進入金山文檔網頁端,點擊我的文檔勾
2023/04/10
(資料圖片)
gcc attribute關鍵字用來給 函數、變量、數據類型設置屬性
C語言代碼里看到__attribute__
這個東西,它就是表示要描述屬性了。gcc的通用屬性可以參考Common-Function-Attributes。
alias格式
alias ("target")
alias屬性可以給target符號起一個別名,兩個名字用起來效果一樣。
extern 類型 新變量名字 __attribute__((alias("舊變量名字")));
extern 類型 新函數名字(參數) __attribute__((alias("舊函數名字")));
備注:新的函數別名需要和舊函數類型相同,即函數返回值和參數要相同;alias后函數名稱只寫函數名字即可,無需攜帶括號和參數。
void __printPath(const char *path)
函數的完整定義,main
函數對其進行了調用,傳值為命令行傳入的文件路徑/home/TEST/BAR/tmp。printPath是一個新名字,它和__printPath是等價的,即它是__printPath的別名。注意因為__printPath帶參數,所以printPath必須帶參數,且類型需和__printPath一致。Debug是一個新名字,它和__myfunc是等價的,即它是__myfunc的別名。date是一個變量別名,與__date等價。 注意這里只能給全局變量起別名。#include < stdio.h >#include < stdlib.h >/* __attribute__((alias()))為函數和變量起別名 *//* printPath(const char* path)為__printPath(const char* path)的別名 */extern void printPath(const char* path) __attribute__((alias("__printPath")));/* Debug()為__myfunc()函數的別名 */extern void Debug() __attribute__((alias("__myfunc")));/* date變量為__date變量的別名 */extern char* date __attribute__((alias("__date")));/* 定義全局變量__date */char* __date="2023-01-18";void __printPath(const char *path){ printf("The %s file in the %s folder.\\n", __FILE__, path);}void __myfunc(){ printf("You are calling the function %s in %s file.\\n",__FUNCTION__, __FILE__);}void printDate(){ fprintf(stdout,"Today is:%s\\n", date);}int main(int argc, char* argv[]){ if(argc != 2) { printf("Please input the correct parameter, it need a parameter with the file path!\\n"); exit(-1); } printf("=========================================================\\n"); printPath(argv[1]); Debug(); printDate(); printf("=========================================================\\n"); return 0;}
編譯程序,執行結果如下:
[root@localhost tmp]# gcc -o alias alias.c[root@localhost tmp]# ./alias /home/TEST/BAR/tmp=========================================================The alias.c file in the /home/TEST/BAR/tmp folder.You are calling the function __myfunc in alias.c file.Today is:2023-01-18=========================================================
注意:如果別名目標(target)與別名不在同一個翻譯單元中定義,則是一個錯誤。在沒有屬性的情況下,GCC假定帶有外部鏈接的不同聲明表示不同的對象。在沒有聲明別名屬性的翻譯單元中,使用別名和別名目標來訪問同一個對象是沒有定義的。此屬性需要匯編程序和對象文件支持,并且可能不適用于所有目標。
標簽: