4 #! \brief Helper macro which appends gcc compatible visibility flags to the
5 #! variable given by RESULT_VAR.
7 #! If supported, the flags -fvisibility=hidden and -fvisibility-inlines-hidden
8 #! will be added. This applies to gcc >= 4.5 and Clang.
11 #! ctkFunctionGetCompilerVisibilityFlags(RESULT_VAR)
16 #! set(myflags "-Werror")
17 #! ctkFunctionGetCompilerVisibilityFlags(myflags)
20 #! The variable \emph myflags will contain the string "-Werror -fvisibility -fvisibility-inlines-hidden"
21 #! if for example gcc 4.6 is used.
23 #! \ingroup CMakeUtilities
24 function(ctkFunctionGetCompilerVisibilityFlags RESULT_VAR)
26 # We only support hidden visibility for gcc for now. Clang 3.0 still has troubles with
27 # correctly marking template declarations and explicit template instantiations as exported.
28 # See http://comments.gmane.org/gmane.comp.compilers.clang.scm/50028
29 # and http://llvm.org/bugs/show_bug.cgi?id=10113
30 set(use_visibility_flags 0)
32 if(CMAKE_COMPILER_IS_GNUCXX)
34 set(use_visibility_flags 1)
35 ctkFunctionGetGccVersion(${CMAKE_CXX_COMPILER} GCC_VERSION)
37 # MinGW does not export all symbols automatically, so no need to set flags.
39 # With gcc < 4.5, RTTI symbols from classes declared in third-party libraries
40 # which are not "gcc visibility aware" are marked with hidden visibility in
41 # DSOs which include the class declaration and which are compiled with
42 # hidden visibility. This leads to dynamic_cast and exception handling problems.
43 # While this problem could be worked around by sandwiching the include
44 # directives for the third-party headers between "#pragma visibility push/pop"
45 # statements, it is generally safer to just use default visibility with
48 if(${GCC_VERSION} VERSION_LESS "4.5" OR MINGW)
49 set(use_visibility_flags 0)
54 if(use_visibility_flags)
55 set(visibility_flags "")
56 ctkFunctionCheckCompilerFlags("-fvisibility=hidden" visibility_flags)
57 ctkFunctionCheckCompilerFlags("-fvisibility-inlines-hidden" visibility_flags)
58 set(${RESULT_VAR} "${${RESULT_VAR}} ${visibility_flags}" PARENT_SCOPE)