【问题标题】:C++11 activation with <target_compile_feature> or <set (CMAKE_CXX_STANDARD)>使用 <target_compile_feature> 或 <set (CMAKE_CXX_STANDARD)> 激活 C++11
【发布时间】:2018-02-27 15:22:55
【问题描述】:

我正在使用一个名为 PyPHS 的 Python 库,专门用于物理建模。 为了在模拟过程中节省计算量,它实现了 C++ 代码生成功能。它使用 CMake 生成特定模拟的可执行文件。

它是在 C++ 11 中实现的。


问题

在 CMakeLists.txt 文件中,C++ 11 功能由以下行激活:

target_compile_features(<project_name> PUBLIC cxx_std_11)

在我的电脑(CMake 3.5.1 & Ubuntu 16.04.4 Xenial Xerus)上,CMake 抛出错误:此功能未知:

-- 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".

在其他安装(Debian 8、Mac OSX 或 windows 7)上没有遇到此错误

修复

我更改了 CMakeLists.txt 模板。这是提交的链接,在我自己的 PyPHS 分支上。

我已将 target_compile_features(&lt;project_name&gt; PUBLIC cxx_std_11) 替换为 set (CMAKE_CXX_STANDARD 11)


问题

这两个命令有什么区别? 您对此事有何见解?我是不是忘了提一些信息?

感谢您的回答!

【问题讨论】:

标签: c++ c++11 cmake


【解决方案1】:

cxx_std_11 编译器元功能在您的CMake 版本中不可用。它是在3.8 中引入的,所以这可以解释错误CMake 3.8 release notes

两者的区别在于target_compile_features()可以针对特定目标请求特定功能。 CMake 将自动将适当的标准应用于指定的目标。另一方面,如果您设置CMAKE_CXX_STANDARD,则所请求的标准(如果 CMake 支持)将应用于项目范围(适用于所有目标)。

如前所述,从CMake 3.8 起,您可以使用target_compile_features() 请求整个标准,在这种情况下,与设置CMAKE_CXX_STANDARD 的唯一区别是该标准仅适用于指定的目标(请阅读下文)。

请注意,如果您使用PRIVATE 范围调用target_compile_features,则请求的功能/标准将应用于指定的目标,如果设置了PUBLIC,则请求的功能/standard 也将应用于依赖于该目标的任何目标。

【讨论】:

猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多