Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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 () 
like image 625
Ying Xiong Avatar asked Dec 07 '14 03:12

Ying Xiong


People also ask

What is CMake Strequal?

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()

What are targets in CMake?

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.

What is option in CMake?

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 ).


1 Answers

Use the TARGET clause of the if command:

conditionally_add (mylib mysrc.cc ${some_condition}) if (TARGET mylib)   # Do something when target found endif() 
like image 105
sakra Avatar answered Sep 28 '22 02:09

sakra