widgg
widgg

Reputation: 1428

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

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

Upvotes: 3

Views: 5349

Answers (1)

Joseph Van Riper
Joseph Van Riper

Reputation: 616

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

Upvotes: 4

Related Questions