GCC/G++:如何为当前平台查找 -march=... 设置
方法 1:显式查找 -march 设置
要确定使用 GCC 或 G++ 时当前平台适合的 -march=... 设置,你可以使用以下命令:
find_march.sh
g++ -march=native -Q --help=target | grep march= | head -n1 | cut -f3-示例输出
find_march_output.txt
znver3在此示例中,输出表示架构为 znver3,对应于 AMD 的 Zen 3 架构。
方法 2:使用 -march=native
如果你在代码将运行的同一机器类型上编译,可以使用 -march=native 选项。这告诉编译器自动检测主机架构并相应地优化代码。
示例命令:
compile_native.sh
g++ -march=native -o my_program my_program.cppIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow