I'm trying to create a custom target in cmake which:
target_compile_options(... PUBLIC ...)
target_include_directories(... PUBLIC ...)
In this way consumers can link against my new target and have requisite include directories and compile options set.
In boost-build you can use the <file>
feature to "wrap" or "alias" a prebuilt library, and add additional "usage requirements" (compiler flags etc) which will be added to all targets using the library.
lib foo
:
: <file>vendor/library.a
:
: <include>vendor
;
vendor/library.a
as a new library foo
. foo
it will also have its include paths updated. foo
will then be able to #include "foo.h"
, and the file will be found because vendor
has been added to the include paths.I'm looking for a way to do the same thing in CMake.
The way I would currently do it would be something along these lines:
find_library(LIB_FOO library.a PATHS ${CMAKE_SOURCE_DIR}/path/to/vendor NO_DEFAULT_PATH)
target_link_library (my_target ${LIB_FOO})
target_include_directories(my_target PRIVATE "${CMAKE_SOURCE_DIR}/path/to/vendor")
However, if there are several targets which need to use foo
then these 3 calls would have be repeated for each of them, which becomes quite onerous.
It would be far easier for consumers of foo
to have some other target which has
target_link_libraries (foo ${CMAKE_CURRENT_LIST_DIR}/vendor/library.a)
target_compile_options (foo PUBLIC ...)
target_include_directories(foo PUBLIC ...)
Question:
Edit in response to comment:
I have tried to create an IMPORTED
library (as described here):
add_library(foo STATIC IMPORTED)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/vendor/library.a)
However, when I then try to set include directories I get an error:
target_include_directories(foo SYSTEM PUBLIC "${CMAKE_CURRENT_LIST_DIR}/vendor")
Error:
CMake Error at vendor/CMakeLists.txt:39 (target_include_directories): Cannot specify include directories for imported target "foo".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With