Ying Xiong
Ying Xiong

Reputation: 4948

How to check whether a target has been added or not?

Assume I have a cmake macro that adds 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 is, after calling this function

conditionally_add (mylib mysrc.cc ${some_condition})

How can I check whether the library has been added? More specifically, I'd like to do something below

if (my_lib_is_added)    # HOW TO DO THIS?
  # Do something.
endif ()

Upvotes: 50

Views: 37632

Answers (1)

sakra
sakra

Reputation: 65981

Use the TARGET clause of the if command:

conditionally_add (mylib mysrc.cc ${some_condition})
if (TARGET mylib)
  # Do something when target found
endif()

Upvotes: 87

Related Questions