[CMake] OBJECT libraries and target visibility properties seem broken

Dan Liew dan at su-root.co.uk
Tue May 19 14:01:08 EDT 2015


Hi,

I'm not sure if this is actually a bug but I've been relying on the
OBJECT library[1] feature as a convenient way of having source code in
multiple directories be put into a shared library. I now have a need
to hide the symbols in my libraries but I've found that using OBJECT
libraries means that setting the CXX_VISIBILITY_PRESET and
VISIBILITY_INLINES_HIDDEN on the final resulting shared library and/or
constituent OBJECT libraries does absolutely nothing.

Here's an example. I've kept everything in a single directory for
simplicity but the real world use case would be that  ``fooX`` and
``barX`` OBJECT libraries are defined in a sub directory.

```
project(objlibtest)
cmake_minimum_required(VERSION 3.0)

add_library(fooX OBJECT
    foo.cpp)

add_library(barX OBJECT
    bar.cpp)

add_library(result SHARED $<TARGET_OBJECTS:fooX> $<TARGET_OBJECTS:barX>)
set_target_properties(result fooX barX PROPERTIES
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN 1)
```

If you watch the build (``make VERBOSE=1``) you'll see that neither
``-fvisibility=hidden`` nor ``-fvisibility-inlines-hidden`` get passed
to the compiler.

If the CMakeLists.txt file is like this

```
project(objlibtest)
cmake_minimum_required(VERSION 3.0)

add_library(result SHARED foo.cpp bar.cpp)
set_target_properties(result PROPERTIES
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN 1)
```

then setting ``-fvisibility=hidden`` and
``-fvisibility-inlines-hidden`` get passed.

Is this a bug or it intentional? If the only purpose of OBJECT
libraries is to provide a convenient way to group source files in
other directories then this seems like a bug.

[1] http://www.cmake.org/Wiki/CMake/Tutorials/Object_Library

Thanks,
Dan.


More information about the CMake mailing list