歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 控制Linux動態鏈接庫導出函數

控制Linux動態鏈接庫導出函數

日期:2017/3/1 9:57:52   编辑: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")))的函數才是對外可見的。

Copyright © Linux教程網 All Rights Reserved