Hello,

I found a strange behavior within the object libraries in the upcoming
CMake 3.12 (I used 3.11.20180519-gdb88f for testing).

If I have for example two object libraries, which are used in one
executable:

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. But if for some reason one object library "links"
to the other and this is used for the executable then it does not work:

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. If I use STATIC Libraries instead it works.
Is this behavior intended? 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 (they are not linked together, instead both are
used on the link line of the executable). A similar solution would be
nice for object libraries, because otherwise the usage of object
libraries which depend on other object libraries is not working well.

Right now to get the compilation working I have to either use the
TARGET_OBJECTS generator expression at the add_executable command, or I
have to link explicit to ObjLib2 for the executable:

add_executable(MyExe main.c $<TARGET_OBJECTS:ObjLib2>)

or

target_link_libraries(MyExe ObjLib1 ObjLib2)

For both possibilities I have to repeat in some sort the dependency and
it gets worse if the depth of the dependencies is going deeper.

Best regards,
Matthias.


-- 

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

Reply via email to