WPS?2019如何清理云空間?WPS 2019如何將PDF轉(zhuǎn)換為WORD?
WPS 2019如何清理云空間?進(jìn)入金山文檔網(wǎng)頁(yè)端,點(diǎn)擊我的文檔勾
2023/04/10
(資料圖片)
gcc attribute關(guān)鍵字用來給 函數(shù)、變量、數(shù)據(jù)類型設(shè)置屬性
C語(yǔ)言代碼里看到__attribute__
這個(gè)東西,它就是表示要描述屬性了。gcc的通用屬性可以參考Common-Function-Attributes。
alias格式
alias ("target")
alias屬性可以給target符號(hào)起一個(gè)別名,兩個(gè)名字用起來效果一樣。
extern 類型 新變量名字 __attribute__((alias("舊變量名字")));
extern 類型 新函數(shù)名字(參數(shù)) __attribute__((alias("舊函數(shù)名字")));
備注:新的函數(shù)別名需要和舊函數(shù)類型相同,即函數(shù)返回值和參數(shù)要相同;alias后函數(shù)名稱只寫函數(shù)名字即可,無需攜帶括號(hào)和參數(shù)。
void __printPath(const char *path)
函數(shù)的完整定義,main
函數(shù)對(duì)其進(jìn)行了調(diào)用,傳值為命令行傳入的文件路徑/home/TEST/BAR/tmp。printPath是一個(gè)新名字,它和__printPath是等價(jià)的,即它是__printPath的別名。注意因?yàn)開_printPath帶參數(shù),所以printPath必須帶參數(shù),且類型需和__printPath一致。Debug是一個(gè)新名字,它和__myfunc是等價(jià)的,即它是__myfunc的別名。date是一個(gè)變量別名,與__date等價(jià)。 注意這里只能給全局變量起別名。#include < stdio.h >#include < stdlib.h >/* __attribute__((alias()))為函數(shù)和變量起別名 *//* printPath(const char* path)為__printPath(const char* path)的別名 */extern void printPath(const char* path) __attribute__((alias("__printPath")));/* Debug()為__myfunc()函數(shù)的別名 */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;}
編譯程序,執(zhí)行結(jié)果如下:
[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=========================================================
注意:如果別名目標(biāo)(target)與別名不在同一個(gè)翻譯單元中定義,則是一個(gè)錯(cuò)誤。在沒有屬性的情況下,GCC假定帶有外部鏈接的不同聲明表示不同的對(duì)象。在沒有聲明別名屬性的翻譯單元中,使用別名和別名目標(biāo)來訪問同一個(gè)對(duì)象是沒有定義的。此屬性需要匯編程序和對(duì)象文件支持,并且可能不適用于所有目標(biāo)。
標(biāo)簽: