[CMake] Resolving inter-library dependencies

Joakim Hove joakim.hove at gmail.com
Mon Jan 30 07:08:46 EST 2012


[ 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?!


More information about the CMake mailing list