[CMake] Creating a library from subdirectories

Chris Johnson cxjohnson at gmail.com
Tue Nov 25 19:35:58 EST 2014


This CMake wiki page (
http://www.cmake.org/Wiki/CMake/Tutorials/Object_Library) claims one can
create a library from subdirectories containing libraries, which is exactly
what I want to do.  However, it doesn't seem to work.  Here's my SSCCE (
http://sscce.org) "toy" example file structure:

.
|-- CMakeLists.txt
|-- component1/
|   |-- CMakeLists.txt
|   `-- c1.c
|-- component2/
|   |-- CMakeLists.txt
|   `-- c2.c
`-- dummy.c


## Here's the top level CMakeLists.txt, per example from Wiki:

cmake_minimum_required(VERSION 2.8.4)
project(lib)
add_subdirectory(component1)
add_subdirectory(component2)
add_library(lib dummy.c)
target_link_libraries(lib    component1 component2)

## The subdirectory CMakeLists.txt files contain effectively just:
add_library(component1  STATIC  c1.c)
## and
add_library(component2  STATIC  c2.c)


## The resulting library file liblib.a contains only the dummy placeholder,
but none of the subdirectory content:

lib$ ar tv liblib.a

rw-r--r--     502/20          696 Nov 25 18:23 2014 dummy.c.o


## Is there some way to make this work?

* Ideally, I would not even need the dummy.c placeholder, but I can live
with that.

* I do not want to list all of the source files in the top level
CMakeLists.txt file.

* 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.


The reasons for my limitations is that to the greatest extent possible, I
am generating CMakeLists.txt files via automation which needs to use a sort
of a least-common-denominator approach to building those files.

..chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20141125/a41d79e1/attachment.html>


More information about the CMake mailing list