ehuang
ehuang

Reputation: 851

External library specific COMPILE_DEFINITIONS in cmake

I've written a cmake module for finding QCustomPlot. However, to use the shared library, one needs to #define QCUSTOMPLOT_USE_LIBRARY. I'd like to provide this define through cmake, automatically adding the definition to any project that uses QCustomPlot.

Here is a snippet of my cmake module and my current attempted solution:

SET(QCP_FOUND "NO")
IF(QCP_LIBRARY AND QCP_INCLUDE_DIR)
  SET(QCP_FOUND "YES")
  SET_PROPERTY(
    GLOBAL
    APPEND
    PROPERTY COMPILE_DEFINITIONS QCUSTOMPLOT_USE_LIBRARY
    )
ENDIF(QCP_LIBRARY AND QCP_INCLUDE_DIR)

However, no linkers append the -DQCUSTOMPLOT_USE_LIBRARY flag in my compilations. What's the right way to approach this problem?

Upvotes: 2

Views: 1281

Answers (1)

user2288008
user2288008

Reputation:

There is no global property COMPILE_DEFINITIONS. But there are such properties for directory, target and source file (see documentation). So probably the closest commands for you are:

set(QCP_FOUND "NO")
if(QCP_LIBRARY AND QCP_INCLUDE_DIR)
  set(QCP_FOUND "YES")
  set_directory_properties(
      PROPERTIES COMPILE_DEFINITIONS QCUSTOMPLOT_USE_LIBRARY
  )
endif()

But I think that this kind of job is for imported targets:

add_library(QCP::qcp UNKNOWN IMPORTED)
set_target_properties(
    QCP::qcp
    PROPERTIES
    IMPORTED_LOCATION "${QCP_LIBRARY}"
    INTERFACE_INCLUDE_DIRECTORIES "${QCP_INCLUDE_DIR}"
    INTERFACE_COMPILE_DEFINITIONS QCUSTOMPLOT_USE_LIBRARY
)

and usage:

add_executable(foo ...)
target_link_libraries(foo PUBLIC QCP::qcp)

Upvotes: 2

Related Questions