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

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

当我尝试 运行 几乎任何代码时,我都会看到此错误消息 想不通为什么

 collect2: error: ld returned 1 exit status

例如当我运行这个简单的代码

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

int main() { 

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

尝试了其他简单的代码,但没有任何效果

您需要使用标志 -fopenmp 编译您的代码。例如:

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

source可以读到:

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).