[CMake] Creating a library from subdirectories

Chris Johnson cxjohnson at gmail.com
Fri Nov 28 17:40:12 EST 2014


Thanks to all who have replied for your efforts in trying to help me.  My
apologies for any lack of clarity in describing my problem.

With the various information and advice you provided, I have been able to
get my CMake configuration working.

Thanks again!

..chris


On Wed, Nov 26, 2014 at 3:30 PM, Stephen Kelly <steveire at gmail.com> wrote:

> Chris Johnson wrote:
>
> > * I do not want to use the add_library(component1 OBJECT
> > ${component1_sources}) and add_library(toplevel
> > $<TARGET_OBJECTS:component1> ... ) syntax if it can be avoided.
>
> Is the constraint that you want a top-level something like
>
>   # All components:
>   set(components component1 component2)
>
>   foreach(comp ${components})
>     add_subdirectory(${comp})
>   endforeach()
>
>   add_library(mylib dummy.c)
>   target_link_libraries(mylib ${components})
>
>   add_executable(other_target main.c)
>   target_link_libraries(other_target mylib)
>
> ?
>
> While at the same time not caring what type the components libraries are
> (OBJECT/STATIC etc)?
>
> A solution for that is in CMake 3.1 by creating an INTERFACE library to
> hide
> the TARGET_OBJECTS expression:
>
>  add_library(component1_objs OBJECT c1.c)
>
>  add_library(component1 INTERFACE)
>  target_sources(component1 INTERFACE
>    $<TARGET_OBJECTS:component1_objs>
>  )
>
> There is a bug however, but I just pushed a fix for it after trying to
> extend your test case to use it:
>
>  http://www.cmake.org/gitweb?p=stage/cmake.git;a=commitdiff;h=672f1001
>
> An INTERFACE library also helps you avoid the dummy.c by not creating the
> intermediate archive at all, if that fits within what you're trying to
> achieve here:
>
>   add_library(mylib INTERFACE)
>   target_link_libraries(mylib INTERFACE ${components})
>
> In the future it may be possible to link to OBJECT libraries directly
> without the INTERFACE library:
>
>  http://public.kitware.com/Bug/view.php?id=14970
>
> Another obvious solution, if it fits within what you're trying to achieve,
> is to avoid the OBJECT libraries instead:
>
>  add_library(component1 INTERFACE)
>  target_sources(component1 INTERFACE
>    ${CMAKE_CURRENT_SOURCE_DIR}/c1.c
>  )
>
> This means that all binary consumers will compile their own version of each
> file, which you probably want to avoid.
>
> Additionally, you do need to specify an absolute path or CMake issues a
> not-
> informative-enough error. A second bug found from this thread, but which I
> haven't yet addressed (but should get done for 3.1 too).
>
> Thanks,
>
> Steve.
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20141128/6953b6d9/attachment.html>


More information about the CMake mailing list