[CMake] Adding Compile Features/Flags for Intel and other Compilers

Ben Morgan bmorgan.warwick at gmail.com
Tue Sep 1 10:02:01 EDT 2015


Hi,

I've been using the new compile features functionality for working with
C++11 and beyond (very useful!!). However, I've run into issues (probably
misunderstandings) trying to extend it to compilers for which CMake doesn't
currently provide a <COMPILERID>-CXX-FeatureTests.cmake file. Of course
long term, these will be added (and I'm happy to contribute here), but on
shorter timescales and to potentially support older CMake 3 series I wanted
to see if I could provide files/settings in my own project to do this.

Initially I thought about providing the file as a module in my project
(e.g. for Intel)
and then prepending the directory holding it to CMAKE_MODULE_PATH. However,
all the files using the feature testing system appear to use absolute paths
(via CMAKE_ROOT) to load the relevant file, so this appears to be a no-go.

I then looked at setting the CMAKE_CXX11_COMPILE_FEATURES and
CMAKE_CXX11_{STANDARD,EXTENSION}_COMPILE_OPTION variables directly after
the initial project call. This appears to work for compile features, but
not for compile options. Here's my simple test case (NB no version checking
of Intel yet...)

cmake_minimum_required(VERSION 3.1)

project(IntelCompileFeatures CXX)


if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")

  message(STATUS "CMAKE_COMPILER_ID matches 'Intel'")

  set(CMAKE_CXX11_COMPILE_FEATURES cxx_auto_type cxx_range_for)

  set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX11_COMPILE_FEATURES})

  set(CMAKE_CXX11_STANDARD_COMPILE_OPTION "-std=c++11")

  set(CMAKE_CXX11_EXTENSION_COMPILE_OPTION "-std=c++11")

endif()


file(WRITE "testIntel.cpp" "

#include <iostream>


int main() {

std::string foo(\"helloworld\");

for (auto& i : foo) {

std::cout << i << std::endl;

}

}

")


add_executable(testIntel testIntel.cpp)

target_compile_features(testIntel PRIVATE

  cxx_auto_type

  cxx_range_for

  )

This will pass the configure/generate stage fine, as CMake configures and
generates recognising the features set on `testIntel`, but the compile
option  -std=c++11 isn't added to the compile flags. I've cross-checked on
the usage of CMAKE_CXX11_{STANDARD,EXTENSION}_COMPILE_OPTION and here I've
been able to modify it after project has been called and see the modified
version
appear in the build time flags.

I'm therefore not sure if I've misunderstood how
CMAKE_CXX11_{STANDARD,EXTENSION}_COMPILE_OPTION is supposed to work, or if
there's some other setting I'm missing. Any help or suggestions gratefully
received!!

Thanks,

Ben Morgan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150901/cebdcc2c/attachment.html>


More information about the CMake mailing list