C++ 17是否提供了跨平台方案来记录编译器版本和Fortran等选项?

dan*_*opa 3 c++ compilation c++17

现代Fortran提供了一些跨平台机制来记录用于构建应用程序的编译器版本和设置.C++ 17有哪些方法可以捕获这些信息?Horton和Van Weert的着作,Beginning C++ 17,似乎没有解决这个问题.

Fortran工具在下面进行了调查.

1.访问编译器版本和选项

iso_fortran_envFortran中提供一种标准方法来访问用于编译代码的编译器版本和设置.随后是一个示例代码段.

代码示例

program check_compiler

use, intrinsic :: iso_fortran_env, only : compiler_options, compiler_version

implicit none

    write ( *, 100 ) "compiler version = ", compiler_version ()
    write ( *, 100 ) "compiler options = ", trim ( compiler_options () )

 100  format ( A, A, / )
      stop "normal termination . . ."

end program check_compiler
Run Code Online (Sandbox Code Playgroud)

样本输出

$ gfortran -o check_compiler check_compiler.f08 
$ ./check_compiler
compiler version = GCC version 8.0.0 20170604 (experimental)

compiler options = -fPIC -mmacosx-version-min=10.12.7 -mtune=core2

STOP normal termination . . .
Run Code Online (Sandbox Code Playgroud)

2.探测主机操作系统并与之交互

Fortran命令喜欢execute_command_line,get_commandget_environment_variable提供另一种在编译时记录信息的路径.

Nic*_*las 7

C++ 17有哪些方法可以捕获这些信息?

没有.C++标准甚至不承认"编译器"或"选项"的概念; 只有"实施".

此外,它实际上没有意义,因为链接到同一程序的不同C++文件可以使用不同的选项进行编译.而且我不只是在谈论DLL/SO; 理论上,您可以静态链接使用不同选项甚至不同编译器版本编译的文件.

不同的编译器有办法通过宏指定它们的版本.但每个人都有自己的报道方式.