COMPILE_LANGUAGE¶
- $<COMPILE_LANGUAGE:languages>¶
This generator expression belongs to the Compiler Language, ID, and Frontend-Variant family. See that section for related generator expressions.
Syntax¶
$<COMPILE_LANGUAGE>
Added in version 3.3.
The compile language of source files when evaluating compile options. See the
related boolean expression $<COMPILE_LANGUAGE:languages> for notes about the portability of this generator
expression.
Syntax¶
$<COMPILE_LANGUAGE:languages>
Added in version 3.3.
Changed in version 3.15: Multiple languages can be specified for languages.
CMake 3.14 and earlier only accepted a single language.
1 when the language used for compilation unit matches any of the
comma-separated entries in languages, otherwise 0. This expression may
be used to specify compile options, compile definitions, and include directories
for source files of a particular language in a target. For example:
add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
target_compile_options(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
target_compile_definitions(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
$<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
)
target_include_directories(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
)
This specifies the use of the -fno-exceptions compile option,
COMPILING_CXX compile definition, and cxx_headers include directory for
C++ only (compiler id checks elided). It also specifies a COMPILING_CUDA
compile definition for CUDA.
Note that with Visual Studio Generators and Xcode there is
no way to represent target-wide compile definitions or include directories
separately for C and CXX languages. Also, with Visual Studio Generators there is no way to represent target-wide flags separately for C
and CXX languages. Under these generators, expressions for both C and C++
sources will be evaluated using CXX if there are any C++ sources and
otherwise using C. A workaround is to create separate libraries for each
source file language instead:
add_library(myapp_c foo.c)
add_library(myapp_cxx bar.cpp)
target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
add_executable(myapp main.cpp)
target_link_libraries(myapp myapp_c myapp_cxx)