什么是等效于“gcc -fvisibility=hidden"的 CMake?控制共享库的导出符号时? [英] What is the CMake equivalent to "gcc -fvisibility=hidden" when controlling the exported symbol of a shared library?
问题描述
我用 C++ 开发了跨平台软件.据我所知,Linux .so 默认导出所有符号,通过 "gcc -fvisibility=hidden"
我可以将所有导出的符号设置为隐藏,然后设置 __attribute__(visibility("default"))
用于我要导出的类和函数,以便我可以控制要导出的内容.
I developed cross platform software in c++. As I know, Linux .so exported all the symbols by default, well through "gcc -fvisibility=hidden"
I can set all the exported symbols as hidden, then set __attribute__(visibility("default"))
for the class and function I want to export, so I can control what I want to export.
我的问题是,使用 CMake,我如何将工作作为gcc -fvisibility=hidden"控件来完成?
My question is, using CMake, how can I do the work as "gcc -fvisibility=hidden" control?
推荐答案
您应该使用当前的 CMake 版本和 <LANG>_VISIBILITY_PRESET 属性.通过这种方式,您可以避免 CMakeLists 中的编译器细节并提高跨平台适用性(避免错误,例如支持 GCC 而不是 Clang).
Instead of setting compiler flags directly, you should be using a current CMake version and the <LANG>_VISIBILITY_PRESET properties instead. This way you can avoid compiler specifics in your CMakeLists and improve cross platform applicability (avoiding errors such as supporting GCC and not Clang).
即,如果您使用的是 C++,您要么调用set(CMAKE_CXX_VISIBILITY_PRESET hidden)
全局设置属性,或 set_target_properties(MyTarget PROPERTIES CXX_VISIBILITY_PRESET hidden)
将设置限制为特定库或可执行目标.如果您使用 C,只需在上述命令中将 CXX 替换为 C.您可能还想调查 VISIBLITY_INLINES_HIDDEN 属性.
I.e., if you are using C++ you would either call
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
to set the property globally, or set_target_properties(MyTarget PROPERTIES CXX_VISIBILITY_PRESET hidden)
to limit the setting to a specific library or executable target. If you are using C just replace CXX by C in the aforementioned commands. You may also want to investigate the VISIBLITY_INLINES_HIDDEN property as well.
GENERATE_EXPORT_HEADER 的文档包含更多提示以及与这两个属性相关的示例.
The documentation for GENERATE_EXPORT_HEADER includes some more tips and examples related to both properties.
这篇关于什么是等效于“gcc -fvisibility=hidden"的 CMake?控制共享库的导出符号时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!