Discussion:
[cmake-developers] Usage Requirements with Object Libraries
Gößwein Matthias / eeas gmbh
2018-04-11 14:48:56 UTC
Permalink
Hello,

Right now I'm using a nightly build (3.11.20180407-g268d0) to use and
test the Usage Requirements with Object Libraries, which is planned for
CMake 3.12.0 (https://gitlab.kitware.com/cmake/cmake/issues/14778)

It's working great so far, but I if i have a construct of two object
libraries, which depend in some way of each other I get an error message:

CMake Error: The inter-target dependency graph contains the following
strongly connected component (cycle):
  "lib1" of type OBJECT_LIBRARY
    depends on "lib2" (strong)
  "lib2" of type OBJECT_LIBRARY
    depends on "lib1" (weak)
At least one of these targets is not a STATIC_LIBRARY.  Cyclic
dependencies are allowed only among static libraries.

CMakeLists.txt:

add_library(lib1 OBJECT lib1.c)
target_link_libraries(lib1 PUBLIC lib2)
add_library(lib2 OBJECT lib2.c)
target_link_libraries(lib2 PUBLIC lib1)


If i use Static libraries, then it works (in fact even if only one
library is an object library it fails). Is it possible to implement the
same thing for object libraries too, or are there some technical
restrictions?
In the past i had sometimes such cyclic dependencies with legacy code
and i think it would be great if it is possible to support such
dependencies with object libraries as well.

Best regards,
Matthias Goesswein.
--
***@eeas.at /mail
www.eeas.at /web
+43 660 1280 131 /phone

------------------------------
eeas gmbh
Technologiepark 17
4320 Perg
Austria

------------------------------
ATU67456549 /uid
FN385458a /firmenbuchnummer
landesgericht linz /firmenbuch
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.
Brad King
2018-04-11 15:45:59 UTC
Permalink
Post by Gößwein Matthias / eeas gmbh
CMake Error: The inter-target dependency graph contains the following
  "lib1" of type OBJECT_LIBRARY
    depends on "lib2" (strong)
  "lib2" of type OBJECT_LIBRARY
    depends on "lib1" (weak)
At least one of these targets is not a STATIC_LIBRARY.  Cyclic
dependencies are allowed only among static libraries.
add_library(lib1 OBJECT lib1.c)
target_link_libraries(lib1 PUBLIC lib2)
add_library(lib2 OBJECT lib2.c)
target_link_libraries(lib2 PUBLIC lib1)
Is it possible to implement the same thing for object libraries too
For reference, the documentation of tll behavior for object libraries is here:

https://cmake.org/cmake/help/git-master/command/target_link_libraries.html#linking-object-libraries

It states that "linking" object libraries to each other only propagates
usage requirements and does nothing special with the object files.
If there are dependencies like this then consumers would need to list
both object libraries together. All object files would be listed once
on the link line and no repetition would be needed since explicitly
listing them guarantees they are linked.

Likely this will be okay. However, I'd like to avoid supporting the
case until we gain more experience with the current design. I've opened
an issue to track it here:

* OBJECT library circular target_link_libraries
https://gitlab.kitware.com/cmake/cmake/issues/17905

Thanks,
-Brad
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.o
Brad King
2018-05-21 14:54:00 UTC
Permalink
I found a strange behavior within the object libraries in the upcoming
CMake 3.12 (I used 3.11.20180519-gdb88f for testing).
Thanks for trying it out!
If I have for example two object libraries, which are used in one
add_library(ObjLib1 OBJECT ObjLib1.c)
target_include_directories(ObjLib1 PUBLIC ...)
add_library(ObjLib2 OBJECT ObjLib2.c)
target_include_directories(ObjLib2 PUBLIC ...)
add_executable(MyExe main.c)
target_link_libraries(MyExe ObjLib1 ObjLib2)
Then this works fine.
Good.
But if for some reason one object library "links"
add_library(ObjLib1 OBJECT ObjLib1.c)
target_include_directories(ObjLib1 PUBLIC ...)
target_link_libraries(ObjLib1 PUBLIC ObjLib2)
add_library(ObjLib2 OBJECT ObjLib2.c)
target_include_directories(ObjLib2 PUBLIC ...)
add_executable(MyExe main.c)
target_link_libraries(MyExe ObjLib1)
I only get the usage requirements of ObjLib2, but not the object files
of ObjLib2 into the executable.
This is expected as things are currently designed. Object files are
only linked when the object library is *directly* referenced by a
target. Only usage requirements are transitive, not the object files.
If I use STATIC Libraries instead it works.
I read the documentation too and i know that
there is no link step for object libraries, but I guess it's the same
for the static libraries
Static libraries have an archiving step rather than a link step and
plays the role of collecting object files together. Object libraries
have no such step.

A major distinction is that listing object files on a link line causes
them to be included in the link unconditionally. We can't simply list
all object library transitive dependencies or their objects may be
included multiple times in one target or duplicated in multiple
dependent targets.
usage of object libraries which depend on other object libraries
is not working well...I have to repeat in some sort the dependency
Yes, and this is because object libraries are meant to be building
blocks for normal libraries and executables. How they are packaged
into those libraries and executables needs to be in full control of
project code. For example, maybe your ObjLib2 objects are supposed
to go into some static library that MyExe links.

-Brad
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake-developers
Loading...