Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 activation with <target_compile_feature> or <set (CMAKE_CXX_STANDARD)>

Tags:

c++

c++11

cmake

I'm using a Python library, named PyPHS, specialized in physical modeling. To save computation during the simulation, it implements a C++ code generation feature. It uses CMake to generate an executable of a particular simulation.

It is implemented in C++ 11.


Issue

In the CMakeLists.txt file, the C++ 11 feature is activated by the following line:

target_compile_features(<project_name> PUBLIC cxx_std_11)

On my computer (CMake 3.5.1 & Ubuntu 16.04.4 Xenial Xerus), CMake throws an error: this feature is unknown:

-- The CXX compiler identification is GNU 5.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:31 (target_compile_features):
  target_compile_features specified unknown feature "cxx_std_11" for target
  "dampedosc".
```

-- Configuring incomplete, errors occurred!
See also "/home/victorw/git/vocal-phs/python/output/dampedosc/CMakeFiles/CMakeOutput.log".

This error has not been encountered on other installs (Debian 8, Mac OSX or windows 7)

Fix

I’ve changed the CMakeLists.txt template. Here is the link to the commit, on my own fork of PyPHS.

I’ve replaced the target_compile_features(<project_name> PUBLIC cxx_std_11) by set (CMAKE_CXX_STANDARD 11)


Question

What is the difference between the two commands? What are your insights on this matter? Did I forget to mention some information?

Thank you for your answers!

like image 525
Vic Tor Avatar asked Jan 03 '23 18:01

Vic Tor


1 Answers

The cxx_std_11 compiler meta-feature is not available in your version of CMake. it was introduced in 3.8, so this would explain the error, CMake 3.8 release notes.

The difference between the two is that target_compile_features() can request specific features for a specific target. CMake will automatically apply the appropriate standard to the specified target. If you on the other hand set CMAKE_CXX_STANDARD, then the requested standard, if supported by CMake, is applied project wide (for all targets).

As mentioned, you can as of CMake 3.8 request an entire standard using target_compile_features(), in which case the only difference from setting CMAKE_CXX_STANDARD is that the standard is only applied to the specified target (read below).

Note that if you invoke target_compile_features with PRIVATE scope, then the requested features/standard will apply only to the specified target, if instead PUBLIC is set, then the requested features/standard will also be applied to any target that depend on that target.

like image 131
thomas_f Avatar answered Jan 05 '23 17:01

thomas_f