CMake Eclipse Build Configurations

I want to generate a Eclipse CDT project with CMake where the resulting Eclipse project contains the defined build types as selectable build configurations from within the IDE.

For example:

if(CMAKE_CONFIGURATION_TYPES)
   set(CMAKE_CONFIGURATION_TYPES PRODUCT_A PRODUCT_B)
   set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
     "Reset the configurations to what we need"
     FORCE)
 endif()

SET(CMAKE_C_FLAGS_PRODUCT_A
    "-DF_ENABLE_FEATURE_A -DF_ENABLE_FEATURE_B
    )

SET(CMAKE_C_FLAGS_PRODUCT_B
    "-DF_ENABLE_FEATURE_A
    )

Using the above approach, a Visual Studio project generator gives me build configuriatons to select product_A product_B and highlights the active code correctly.

If however I generate a Eclipse project the build configuration isn't there.

How do I get this to work for Eclipse projects?


Short answer: you don't.

The Eclipse CDT generator creates a wrapper around generated Makefiles. Makefile-based generators can't be made to be multi-configuration.

You'll have to use separate binary trees (note that both can refer back to the same source tree), and use something like options to enable product A and/or product B:

OPTION(PRODUCT_A "Build product A." OFF)
OPTION(PRODUCT_B "Build product B." OFF)
IF(PRODUCT_A AND PRODUCT_B)
  MESSAGE(SEND_ERROR "Cannot build both product A and B at the same time.")
ENDIF()

IF(PRODUCT_A)
  SET(CMAKE_C_FLAGS
    "${CMAKE_C_FLAGS} -DF_ENABLE_FEATURE_A -DF_ENABLE_FEATURE_B"
  )
ENDIF()

IF(PRODUCT_B)
  SET(CMAKE_C_FLAGS
    "${CMAKE_C_FLAGS} -DF_ENABLE_FEATURE_A"
  )
ENDIF()
链接地址: http://www.djcxy.com/p/47752.html

上一篇: MySQLi准备好的语句?

下一篇: CMake Eclipse构建配置