CMake - search in list - "Unknown arguments specified"

Hello community :slight_smile:

I’m currently facing a cmake code issue, which I’m trying to understand and might need some support here. So I hope this is the right place to address this.

The following code should detect the used compiler and decide if it’s supported by my library or not.

cmake_minimum_required(VERSION 3.17)

# not important stuff

# supported compilers

list(APPEND SUPPORTED_COMPILERS "GNU")
list(APPEND SUPPORTED_COMPILERS "Clang")

# check for compiler support

message("${PROJECT_NAME}: Check if compiler is supported...")

if(${CMAKE_CXX_COMPILER_ID} IN_LIST ${SUPPORTED_COMPILERS})
    message("${PROJECT_NAME}: \"${SELECTED_COMPILER_SUPPORTED}\" is supported...")
else()
    message("${PROJECT_NAME}: \"${CMAKE_CXX_COMPILER_ID}\" is not supported...")
    return()
endif()

This unfortunately produces the following error message when loading the CMake project:

CMake Error at CMakeLists.txt:36 (if):
  if given arguments:

    "GNU" "IN_LIST" "GNU" "Clang"

  Unknown arguments specified

So here is what I understand:

  • both the list and the compiler variable are not empty, so the logic seems kind of working
  • GNU is even the compiler I’d like to use
  • Checking the IN_LIST documentation states that this is supported with CMake version 3.3
  • I also tried setting the cmake policy CMP0057 to NEW - which I think isn’t even necessary with my used CMake version? (I’m running CMake 3.20.2, which comes as bundled with CLion.)

If you guys could support me here, I’d really appreciate it.
So thanks in advance,

Patrick

The documentation says:
if(<variable|string> IN_LIST <variable>)
which basically means that after IN_LIST comes the NAME of the variable containing the list. Not a list itself (which is what you did by expanding the variable)

Thank you, replacing the variable by the name solved the issue :slight_smile:

if(${CMAKE_CXX_COMPILER_ID} IN_LIST SUPPORTED_COMPILERS)