[CMake] Resolving inter-library dependencies

David Cole david.cole at kitware.com
Mon Jan 30 07:52:04 EST 2012


On Mon, Jan 30, 2012 at 7:08 AM, Joakim Hove <joakim.hove at gmail.com> wrote:
> [ Sorry if this is a duplicate - I did not get a copy myself ?]
>
> Hello;
>
> I am trying to us CMake to build a fairly large C project on Linux. I
> am using CMake version 2.6.
>
> The project is organised with several libraries and also many
> executable programs - I build the libraries both as shared and static
> libraries, and create two versions of the executable programs (linking
> to the shared and static libraries respectively). I have organized
> things with many different CMakeLists.txt files as follows:
>
>
> project/CMakeLists.txt                             <- Project root
> #----
> project/lib1/CMakeLists.txt                      <- Root of lib1
> project/lib1/src/CMakeLists.txt                 <- Src directory of lib1
> project/lib1/applications/CMakeLists.txt   <- Directory containing
> small utilities based on lib1
> #----
> project/lib2/CMakeLists.txt                      <- Root of lib2
> project/lib2/src/CMakeLists.txt                 <- Src directory of lib2
> project/lib2/applications/CMakeLists.txt   <- Directory containing
> small utilities based on lib2
> #-----
> project/lib3/CMakeLists.txt                      <- Root of lib3
> project/lib3/src/CMakeLists.txt                 <- Src directory of lib3
> project/lib3/applications/CMakeLists.txt   <- Directory containing
> small utilities based on lib3
> #-----
> ....
> #-----
> project/app/src/CMakeLists.tx                 <- src of main
> application - linking to all the libraries lib1, lib2, lib3 , ....
>
>
> In each of the librarry folders I have:
>
> add_library( libn_static     STATIC   libn_source )
> add_library( libn_shared  SHARED libn_source )
>
> set_target_properties( libn_static    PROPERTIES OUTPUT_NAME libn )
> set_target_properties( libn_shared PROPERTIES OUTPUT_NAME libn )
>
>
>
> In the application folders I have:
>
> add_executable( appn_static  appn_source )
> target_link_librarries( appn_static lib1_static lib2_static)
> add_executable( appn_shared  appn_source )
> target_link_librarries( appn_shared lib1_shared lib2_shared)
>
>
> Now - this setup very nearly works. The problem is that when I come to
> linking (only to the shared I think ...?) libraries I have built
> myself I will get the output:
>
> make[2]: *** No rule to make target `lib1/src/liblib1.so', needed by
> `lib1l/applications/app1'.  Stop.
> make[1]: *** [libecl/applications/CMakeFiles/convert.x.dir/all] Error 2
>
> This is obviously "wrong" - because make has written:
>
> Linking C shared library liblib1.so
>
> On the previous lines -i.e. it knows how to make the liblib1.so
> librarry - and has actually done so as well. If i just type 'make' one
> more time the linker/make file will pick up the liblib1.so file
> correctly built in the preceeding attempt, and correctly build my
> application. I.e. it seems to me that resolving of library
> dependencies only takes the libraries present when make is invoked
> into account; and does not consider the libraries built during the
> process?
>
> I have tried several versions of: add_dependencies() -
> target_link_libraries() and link_directory() - but more or less no
> matter what I do the result seems to be the same: clean build will
> fail, when I reissue build commands the libraries created in the
> previous step will be picked up and the complete build will succeed.
>
> Any tips?!
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


The latest version of CMake 2.6 was v2.6.4, tagged on Apr 28, 2009,
nearly 3 years ago now.

Please try with an more up-to-date build of CMake (v2.8.7 is the
current release), there have been literally hundreds of bugs fixed
since 2.6.4.

If your distro does not provide an easy way to get CMake, you can
download pre-built binaries from
http://www.cmake.org/files/v2.8/?C=M;O=D (untar and run from anywhere,
no need for root) or you can easily build it yourself via CMake's
bootstrap script.


HTH,
David


More information about the CMake mailing list