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

时间:2020-01-30 07:56:59

标签: c gcc visual-studio-code openmp

我已经在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

2 个答案:

答案 0 :(得分:2)

gcc openmp_helloword.c -o openmp_helloword

链接期间缺少库。 gcc将使用-fopenmp为您添加它们。考虑一下文档:

https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-fopenmp

答案 1 :(得分:1)

您必须在vs代码选项中启用openmp

右键单击您的项目,选择“属性”,然后选择C / C ++,然后选择“语言”,然后将“ OpenMP支持”更改为“是”。

它应将选项-fopenmp添加到编译选项中。