[CMake] OBJECT libraries and transient include dirs

Wolf wolf at wolfsden.cz
Mon Aug 8 11:56:28 EDT 2016


Hi all,

I'm trying to setup OBJECT library for the first time and I've run into
problem that include directories are not transient. Let me illustrate:

	add_library(foo-object OBJECT src/cache.cpp)
	target_include_directories(foo-object PUBLIC include)

	add_library(foo SHARED $<TARGET_OBJECTS:foo-object>)
	add_library(foo_static STATIC $<TARGET_OBJECTS:foo-object>)

This compiles just fine. However, when I try to use it:

	add_executable(bar src/main.cpp)
	target_link_libraries(bar foo)

with `main.cpp` looking like this:

	#include "cache.h"
	int main(int, char**) { return 0; }

This doesn't compile, because the include directory from `foo-object`
isn't carried over.

This seem like slight oversight when designed the OBJECT functionality,
would it be possible to fix this in some future version? Or is there
some good reason it is like it is?

Have a nice day,
W.

PS: I've come up with following workaround, it should work right?

	add_library(foo-object OBJECT src/cache.cpp)
	target_include_directories(foo-object PUBLIC include)

	get_property(object_include_dirs TARGET foo-object PROPERTY INCLUDE_DIRECTORIES)
	get_property(object_link_libs TARGET foo-object PROPERTY LINK_LIBRARIES)

	add_library(foo SHARED $<TARGET_OBJECTS:foo-object>)
	target_include_directories(foo PUBLIC ${object_include_dirs})
	target_link_libraries(foo PUBLIC ${object_link_libs})

	add_library(foo_static STATIC $<TARGET_OBJECTS:foo-object>)
	target_include_directories(foo_static PUBLIC ${object_include_dirs})
	target_link_libraries(foo_static PUBLIC ${object_link_libs})

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160808/7e5848f2/attachment.sig>


More information about the CMake mailing list