如何在vs code中安装和使用Openmp

Shu*_*ral 1 c gcc openmp visual-studio-code

我已经在 vscode 中正确安装并运行了 c/c++,我的 gcc 版本是 8.2.0 并且我已经安装了 MinGw

我使用 VS code 来运行我的 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这没有用,因为

[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
Run Code Online (Sandbox Code Playgroud)

ema*_*uts 5

gcc openmp_helloword.c -o openmp_helloword

您在链接过程中缺少库。gcc 将为您添加它们-fopenmp。考虑文档:

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