Search code examples
cmultithreadingparallel-processingopenmp

Getting "collect2: error: ld returned 1 exit status"


I see this error message when I try to run almost any code cant figure out why

 collect2: error: ld returned 1 exit status

for example when i run this simple code

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

int main() { 

  printf ("Hello, world:"); 
  #pragma omp parallel 
    printf (" %d", omp_get_thread_num ()); 
  printf ("\n"); 
  return 0; 
}

tried other simple codes but nothing is working


Solution

  • You need to compile your code with the flag -fopenmp. For example:

    gcc -fopenmp -O3 -w -Wall -pedantic main.c
    

    From source one can read:

    Enabling OpenMP

    To activate the OpenMP extensions for C/C++ and Fortran, the compile-time flag -fopenmp must be specified. This enables the OpenMP directive #pragma omp in C/C++ and !$omp directives in free form, c$omp, *$omp and !$omp directives in fixed form, !$ conditional compilation sentinels in free form and c$, *$ and !$ sentinels in fixed form, for Fortran. The flag also arranges for automatic linking of the OpenMP runtime library (Runtime Library Routines).