'OpenMP Hello World Error: "Undefined symbols for archtecture x86_64: ... "

I have the following basic Hello World program for using OpenMP:

#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/time.h>
#include<omp.h>

int main(int argc, char **argv) {
    printf("This line is executed serially.\n\n");

    #pragma omp parallel
        int nthreads = omp_get_num_threads();
        int myid = omp_get_thread_num();
        printf("Hello, world! I am thread %d of %d treads\n", myid, nthreads);
  
  return EXIT_SUCCESS;
}

I am using Microsoft Visual Studio Code as an IDE, and the code is written in c (and file therefore has a .c, not .cpp extension). When I run the code without debugging, VSC uses the following command in terminal to run the code, and the following error appears:

gcc tescript1.c -o tescript1 && "/[insert_working_directory_here]/"tescript1

Undefined symbols for architecture x86_64:
  "_omp_get_num_threads", referenced from:
      _main in tescript1-79e487.o
  "_omp_get_thread_num", referenced from:
      _main in tescript1-79e487.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Other threads including C++ Hello World Undefined symbols for architecture x86_64: seem to suggest I'm using the wrong compiler. However, in this example, their program is c++, not c, and VSC's settings are anything but clear on how to change compilers. I've ended up stuck without being able to try the suggestions I've read or 100% for sure identify the problem. Any help is appreciated.



Solution 1:[1]

You could try to set the /openmp value to OpenMP support in Visual studio project properties as shown below.

project properties

As you can see I was able to run a basic helloworld program in C with openMP support.

basic helloworld in c with openMP

<sys/time.h> is a POSIX header, not part of the C/C++ standard library. It won't work in windows. Use just <time.h> if you are building for windows.

Also, I hope you have not missed, enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1