gpt4 book ai didi

c++ - 使用和不使用 fopenmp 标志编译 C++ 代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:08:09 26 4
gpt4 key购买 nike

我已经安装了 C++ 和 Fortran 编译器并测试了 (C++) 程序(串行和并行版本)。

在没有启用 -openmp 选项的情况下编译(并行化)代码时,在 Fortran 中应该以(默认)串行模式编译代码

但在 C++ 中我得到类似的错误对 omp_get_thread_num

的 undefined reference

但是在 Fortran 中,当您编译没有 -fopenmp 标志的代码时,它会忽略任何以 $!omp 开头的代码,例如

$!omp id = omp_get_thread_num()

C++中有类似的选项吗?

最佳答案

您可以使用 _OPENMP 定义来保护您的 C/C++ 代码。这样,只要您的应用程序未链接到它,您就可以避免引入对 OpenMP 运行时的调用。

例如,你可以有下面的代码

void foo (void)
{
#ifdef _OPENMP
printf ("I have been compiled with OpenMP support\n");
#else
printf ("I have been compiled without OpenMP support\n");
#endif
}

关于c++ - 使用和不使用 fopenmp 标志编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38868932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com