How to check if a target has been added or not?
Suppose I have a cmake macro that adds a target (library or executable) based on some conditions
macro (conditionally_add target_name target_src condition)
if (condition)
add_library (target_name target_src)
endif ()
endmacro()
My question: after calling this function
conditionally_add (mylib mysrc.cc ${some_condition})
How to check if a library has been added? In particular, I would like to do something below
if (my_lib_is_added) # HOW TO DO THIS?
# Do something.
endif ()
+3
Ying xiong
source
to share
2 answers
Use the TARGET
command suggestion if
:
conditionally_add (mylib mysrc.cc ${some_condition})
if (TARGET mylib)
# Do something.
endif()
+12
sakra
source
to share
There doesn't seem to be a way to iterate over targets in CMake so far, so you'll need to do it yourself.
You need to create a custom variant of the functions add_executable()
and add_library()
that will do something like
function(my_add_executable TARGET)
list(APPEND MY_TARGETS ${TARGET})
add_executable(${TARGET} ${ARGN}
endfunction()function(my_add_executable TARGET)
0
arrowd
source
to share