[CMake] Handling library interdependencies

Tyler Roscoe tyler at cryptio.net
Tue Jun 16 17:55:31 EDT 2009


On Tue, Jun 16, 2009 at 03:41:51PM -0500, Mark Lohry wrote:
> I have a number of subdirectories, each containing source to be built into
> their own libraries, which contain many interdependencies. I'm trying to
> build an executable to link against these libraries, and I'm getting a
> console full of "undefined reference to..." errors on linking step. The
> libraries and executable source all compile individually without issue.
> 
> Structure is like so:
> 
> # top-level CMakeLists.txt
> project( proj )
> ADD_SUBDIRECTORY( lib )
> ADD_SUBDIRECTORY( src )
> 
> 
> # /lib/CMakeLists.txt
> ADD_SUBDIRECTORY( lib1 )
> ADD_SUBDIRECTORY( lib2 )
> 
> 
> # /lib/lib1/CMakeLists.txt
> include_directories( ${proj_SOURCE_DIR}/lib )
> set( lib1_SRCS
>   ..... bunch of c++ sources )
> add_library( lib1 SHARED ${lib1_SRCS} )
> 
> 
> # /lib/lib2/CMakeLists.txt
> include_directories( ${proj_SOURCE_DIR}/lib )
> set( lib2_SRCS
>   ..... bunch of c++ sources )
> add_library( lib2 SHARED ${lib2_SRCS} )
> 
> 
> # src/CMakeLists.txt
> include_directories( ${proj_SOURCE_DIR}/lib )
> set( execthing_SRCS
>  .... bunch of C++ sources )
> add_executable( execthing ${execthing_SRCS} )
> target_link_libraries( execthing lib1 lib2 )

This all looks reasonable to me, so the devil is probably in the
details. Are you sure all your paths make sense? Can you use nm or
dumpbin or whatever to see the "undefined" symbols in the libraries
where you expect to find them?

So if you build the libraries individually and then build the executable
individually, everything works? You might try playing with some explicit
add_dependencies() to see if there's some kind of build order problem.
CMake should be smart enough to figure this out from your
target_link_libraries, but maybe you have something strange going on
under the covers?

tyler


More information about the CMake mailing list