gcc 如何在vs代码中安装和使用Openmp

zmeyuzjn  于 2022-12-27  发布在  其他
关注(0)|答案(3)|浏览(386)

我已经在vscode中正确安装并运行了c/c++,我的gcc版本是8.2.0,并且我已经安装了MinGw
我使用VS代码来运行我的C程序

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(int argc, char* argv[])
{
    int nthreads, tid;
    {
        tid = omp_get_thread_num();
        printf("welcome to GFG from thread = %d\n", tid);
        if (tid == 0){
            nthreads = omp_get_num_threads();
            printf("number of threads = %d\n", nthreads);
        }
    }
}

但没有成功

[Running] cd "c:\cexam\" && gcc openmp_helloword.c -o openmp_helloword && "c:\cexam\"openmp_helloword
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0xf): undefined reference to `omp_get_thread_num'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0x33): undefined reference to `omp_get_num_threads'
collect2.exe: error: ld returned 1 exit status
jhiyze9q

jhiyze9q1#

gcc打开mp_用户名. c-o打开mp_用户名
链接过程中缺少库。gcc将使用-fopenmp为您添加这些库。请参考文档:
https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-fopenmp

xriantvc

xriantvc2#

您必须在vs代码选项中启用openmp
右键单击您的项目,选择“属性”,然后选择C/C++,然后选择语言,并将“OpenMP支持”更改为“是”。
它应该将选项-fopenmp添加到编译选项中。

rxztt3cl

rxztt3cl3#

在我的tasks.json中我添加了一行:“-fopenmp”,在args下,如下所示:

...

"tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "W:\\Programs\\TDM-GCC-64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-fopenmp",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],

...

相关问题