控制linux動態連結程式庫匯出函數
在linux中,我們可以通過-fvisibility=default|internal|hidden|protected來控制匯出函數。
在GCC協助文檔 -fvisibility=default|internal|hidden|protected參數下有這樣一段描述:
a superior solution made possible by this option to marking things hidden when the default is public is to make the default hidden and mark things public. This is the norm with DLL's on Windows and with -fvisibility=hidden and "__attribute__ ((visibility("default")))" instead of "__declspec(dllexport)" you get almost identical semantics with identical syntax. This is a great boon to those working with cross-platform projects.
需要瞭解的是,在linux下,源檔案中的所有函數都有一個預設的visibility屬性,即為public,在編譯命令中加入 -fvisibility=hidden參數,會將所有預設的public的屬性變為hidden。此時,如果對函數設定__attribute__ ((visibility("default")))參數,使特定的函數仍然按預設的public屬性處理,則-fvisibility=hidden參數不會對該函數起作用。所以,設定了-fvisibility=hidden參數之後,只有設定了__attribute__ ((visibility("default")))的函數才是對外可見的。