In C++, visibility attributes are used to control the visibility of symbols in shared libraries or dynamic link libraries (DLLs). This is particularly useful to prevent certain functions or classes from being exposed to other components, ensuring better encapsulation of the code and reducing potential name clashes.
To hide symbols in a shared library, you can use the `__attribute__((visibility("hidden")))` attribute in GCC or Clang. Here’s how you can implement this:
// Example of hiding a symbol in C++
#include <iostream>
// Marking the function with hidden visibility
__attribute__((visibility("hidden"))) void hiddenFunction() {
std::cout << "This function is hidden." << std::endl;
}
void visibleFunction() {
std::cout << "This function is visible." << std::endl;
}
int main() {
visibleFunction();
// Uncommenting the next line will cause a link error if hiddenFunction is not exposed
// hiddenFunction();
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?