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 ()
According to the CMake documentation, the STREQUAL comparison is allowed to take either a VARIABLE or a STRING as either parameter. So, in this example below, the message does NOT print, which is broken: set( FUBARTEST "OK" ) if( FUBARTEST STREQUAL "OK" ) message( "It Worked" ) endif()
Targets. In general, targets comprise executables or libraries which are defined by calling add_executable or add_library and which can have many properties set. They can have dependencies on one another, which for targets such as these just means that dependent ones will be built after their dependencies.
Provide a boolean option that the user can optionally select. option(<variable> "<help_text>" [value]) If no initial <value> is provided, boolean OFF is the default value. If <variable> is already set as a normal or cache variable, then the command does nothing (see policy CMP0077 ).
Use the TARGET
clause of the if
command:
conditionally_add (mylib mysrc.cc ${some_condition}) if (TARGET mylib) # Do something when target found endif()
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