Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly set the visibility property in CMake with gcc?

Tags:

gcc

cmake

I tried a couple of options to properly set the visibility in my library. Like:

target_compile_options(MY_LIB PRIVATE "-fvisibility=hidden")
set_target_properties(MY_LIB PROPERTIES C_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1)
set_target_properties(MY_LIB PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1)

or

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")

And both don't work. I have the specific functions that I want to expose set inside a block

#pragma GCC visibilty push(default)
  void thisShouldBeTheOnlyThingVisible(void) {
    ...
  }
#pragma GCC visibilty pop

But, if I run different tools, like objdump or readelf, not only I'm seeing all the internal functions that should not be listed, I also see file names and non static variable names. I wouldn't mind in debug mode, but in release, it shouldn't be there.

What's the right process here to get everything that I want hidden except those special functions?

Thanks

W

like image 753
widgg Avatar asked Sep 01 '25 02:09

widgg


1 Answers

I think you're looking for:

add_library(MY_LIB ${MY_SOURCES})
set_target_properties(MY_LIB PROPERTIES
  C_VISIBILITY_PRESET hidden
  VISIBILITY_INLINES_HIDDEN ON
)

Source: CMake's test

like image 174
Joseph Van Riper Avatar answered Sep 02 '25 21:09

Joseph Van Riper