[CMake] Handling library interdependencies

Mark Lohry mlohry at gmail.com
Tue Jun 16 19:22:22 EDT 2009


Thanks for the reply.

I added "add_dependencies( lib1 lib2 )", "add_dependencies( lib2 lib1 )",
and "add_dependencies( execthing lib1 lib2 )" in their appropriate places,
and changed the libraries to STATIC, but no change.

Yes, everything compiles when i build the libraries and executables
separately; it fails on the link step after this, with "undefined reference"
to every library function used.

I'm unfamiliar with 'nm', can you be more specific as to what I should be
looking for?


On Tue, Jun 16, 2009 at 4:55 PM, Tyler Roscoe <tyler at cryptio.net> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090616/222fb0ec/attachment.htm>


More information about the CMake mailing list