Define multiple exclusive macros in CMake

My program needs FOO or BAR macro defined, but not both. By default, FOO is defined. Following is the snippet of CMakeList.txt:

OPTION(BAR "BAR" OFF)
OPTION(FOO "FOO" ON)
IF(BAR)
    SET(FOO OFF CACHE BOOL "Use BAR" FORCE)
    MESSAGE(STATUS "Use BAR\n")
    ADD_DEFINITIONS(-DBAR)
ELSE()
    MESSAGE(STATUS "Use FOO\n")
    ADD_DEFINITIONS(-DFOO)
ENDIF()

Notice: for simplicity, I just use the same name for macro and option variable. It may not be what you want.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.