Search code examples
cgccvisual-studio-codeopenmp

How to install and use Openmp in vs code


I have install and run correctly c/c++ in vscode, my gcc version is 8.2.0 and I have installed MinGw

I use VS code to run my C program

#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);
        }
    }
}

but it didn't work because

[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

Solution

  • gcc openmp_helloword.c -o openmp_helloword

    You are missing libraries during link. gcc will add them for you with -fopenmp. Consider the documentation:

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