MantisBT - CMake
View Issue Details
0015556CMakeCMakepublic2015-05-06 06:112016-02-01 09:10
Kjell Irgens 
Brad King 
normalminoralways
closedfixed 
FedoraLinux4.0
CMake 3.2.1 
CMake 3.3CMake 3.3 
0015556: CMAKE_<lang>_VISIBILITY_PRESET not used when compiling static libraries
When compiling a STATIC library the -fvisibility=hidden compiler flag is not set to gcc when compiling.

Why is this a problem? I am linking the resulting static library into another SHARED library, and I want the symbols defined in the link library to be hidden, which is why I used CMAKE_CPP_VISIBILITY_PRESET in the first place.

https://gcc.gnu.org/wiki/Visibility [^]
"In your build system (Makefile etc), you will probably wish to add the -fvisibility=hidden and -fvisibility-inlines-hidden options to the command line arguments of every GCC invocation"

The workaround is to add -fvisibility=HIDDEN as to COMPILE_FLAGS.
set (CMAKE_C_VISIBILITY_PRESET hidden)

add_library (mplug STATIC test.c)

make VERBOSE=1
No tags attached.
Issue History
2015-05-06 06:11Kjell IrgensNew Issue
2015-05-06 08:44Robert MaynardNote Added: 0038719
2015-05-06 10:04Kjell IrgensNote Added: 0038720
2015-05-06 14:02Brad KingNote Added: 0038723
2015-05-15 04:45Kjell IrgensNote Added: 0038750
2015-05-26 09:25Brad KingNote Added: 0038799
2015-05-26 09:25Brad KingAssigned To => Brad King
2015-05-26 09:25Brad KingStatusnew => resolved
2015-05-26 09:25Brad KingResolutionopen => fixed
2015-05-26 09:25Brad KingFixed in Version => CMake 3.3
2015-05-26 09:25Brad KingTarget Version => CMake 3.3
2016-02-01 09:10Robert MaynardNote Added: 0040402
2016-02-01 09:10Robert MaynardStatusresolved => closed

Notes
(0038719)
Robert Maynard   
2015-05-06 08:44   
As far as I am aware the visibility flags have no effect on static libraries since there is no real 'linking' step.
(0038720)
Kjell Irgens   
2015-05-06 10:04   
Too bad, as -fvisibility definitely have an effect on building object files
(command line arguments of *every* GCC invocation)

I just found out that it also does not seem to have any effect when building a MODULE, although I have not testet this properly.
(0038723)
Brad King   
2015-05-06 14:02   
For reference, the code that decides whether to add flags based on the target type is here:

 http://www.cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmLocalGenerator.cxx;hb=v3.2.2#l2369 [^]

As of CMake 3.2.2 it adds them for SHARED and MODULE libraries and executables with ENABLE_EXPORTS set.
(0038750)
Kjell Irgens   
2015-05-15 04:45   
You are right, it works for MODULE. The original problem remains though.
(0038799)
Brad King   
2015-05-26 09:25   
I added a policy to enable this for all target types in a compatible way:

 Honor visibility properties for all target types
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=700f1c3b [^]
(0040402)
Robert Maynard   
2016-02-01 09:10   
Closing resolved issues that have not been updated in more than 4 months.