[CMake] Creating a library from subdirectories

Chris Johnson cxjohnson at gmail.com
Wed Nov 26 14:45:33 EST 2014


I know that one cannot link static libraries together; they're just
archives of objects, really.  But since CMake abstracts the actual
construction of the library, it could conceivably know how to do the right
thing and collect all the needed objects recursively, as it appears to do
for executables.  It seems it does not know how to do this.  Hence, I
believe the section of the wiki quoted below is thus in error, as it
implies this kind of transitive linking resolution can be done.  I've made
it work using OBJECT libraries, but as I originally mentioned, I'm really
trying for the simplest, most basic kind CMakeLists.txt syntax possible so
as to allow my automation to do most of the work (I have a very large
source tree).  Thanks.

Quote:

For example, we can create two subdirectories:

# A/CMakeLists.txt
add_library(A ${A_srcs})

# B/CMakeLists.txt
add_library(B ${B_srcs})

and then refer to those libraries from the top directory:

# CMakeLists.txt
add_subdirectory(A)
add_subdirectory(B)
add_library(big ${other_srcs})
target_link_libraries(big A B)

This approach is easy to use and helps organize the project source tree.
Since CMake tracks link dependencies automatically it is easy to refer to
the "big" library in target_link_libraries calls elsewhere in the project
and let CMake propagate the dependency on A and B.




On Wed, Nov 26, 2014 at 6:52 AM, Mueller-Roemer, Johannes Sebastian <
Johannes.Sebastian.Mueller-Roemer at igd.fraunhofer.de> wrote:

>  Sorry overread the last point in your mail. I don’t believe there is a
> way to avoid this directly. What you seem to want is what Xcode calls a
> prelinked library (http://cocoadev.com/SingleObjectPrelink) . CMake does
> not have such an option as far as I know
>
>
>
> --
>
> Johannes S. Mueller-Roemer, MSc
>
> Wiss. Mitarbeiter - Interactive Engineering Technologies (IET)
>
>
>
> Fraunhofer-Institut für Graphische Datenverarbeitung IGD
>
> Fraunhoferstr. 5  |  64283 Darmstadt  |  Germany
>
> Tel +49 6151 155-606  |  Fax +49 6151 155-139
>
> johannes.mueller-roemer at igd.fraunhofer.de  |  www.igd.fraunhofer.de
>
>
>
> *From:* Mueller-Roemer, Johannes Sebastian
> *Sent:* Wednesday, November 26, 2014 13:50
> *To:* 'Chris Johnson'; cmake at cmake.org
> *Subject:* RE: [CMake] Creating a library from subdirectories
>
>
>
> In the example on that site an OBJECT library is created (the output
> consists of multiple .obj files), you used STATIC libraries instead (which
> each output a single .lib)
>
>
>
> --
>
> Johannes S. Mueller-Roemer, MSc
>
> Wiss. Mitarbeiter - Interactive Engineering Technologies (IET)
>
>
>
> Fraunhofer-Institut für Graphische Datenverarbeitung IGD
>
> Fraunhoferstr. 5  |  64283 Darmstadt  |  Germany
>
> Tel +49 6151 155-606  |  Fax +49 6151 155-139
>
> johannes.mueller-roemer at igd.fraunhofer.de  |  www.igd.fraunhofer.de
>
>
>
> *From:* CMake [mailto:cmake-bounces at cmake.org <cmake-bounces at cmake.org>] *On
> Behalf Of *Chris Johnson
> *Sent:* Wednesday, November 26, 2014 01:36
> *To:* cmake at cmake.org
> *Subject:* [CMake] Creating a library from subdirectories
>
>
>
> 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/20141126/70ae33c3/attachment.html>


More information about the CMake mailing list