C - 未引用的Omp功能

时间:2017-05-11 11:36:45

标签: c openmp

我正在使用Omp进行'C'并行编程。在我的模块 array-sum-parallel.c 中,我首先按要求包含文件 hpc.h ,该文件位于我的C文件的同一文件夹中并包含以下代码:

/*This header file provides a function double hpc_gettime() that returns the elaps  
ed time (in seconds) since "the epoch". The function uses the timing routing of th  
e underlying parallel framework (OpenMP or MPI), if enabled; otherwise, the defaul  
t is to use the clock_gettime() function.

IMPORTANT NOTE: to work reliably this header file must be the FIRST header file th  
at appears in your code.*/

#ifndef HPC_H  
#define HPC_H  
#if defined(_OPENMP)  
#include <omp.h>  
/* OpenMP timing routines */

double hpc_gettime( void )
{
    return omp_get_wtime();
}

#elif defined(MPI_Init)  
/* MPI timing routines */  
double hpc_gettime( void )
{
    return MPI_Wtime();
}

#else  
/* POSIX-based timing routines */  
#if _XOPEN_SOURCE < 600  
#define _XOPEN_SOURCE 600  
#endif  
#include <time.h>  

double hpc_gettime( void )
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts );
    return ts.tv_sec + (double)ts.tv_nsec / 1e9;
}  
#endif  
#endif

然后我包含 omp.h,stdio.h,stdlib.h ,我使用 gcc -c -Wall -Wpedantic -fopenmp 进行编译,这没关系,但是当我链接gcc -o -fopenmp array-sum-parallel array-sum-parallel.o我收到此错误: array-sum-parallel.c :(。text + 0x9):对omp_set_num_threads 的未定义引用以及所有其他Omp函数。为什么呢?

1 个答案:

答案 0 :(得分:2)

您必须将已编译的文件与omp库链接,您可以使用g ++编译并使用单个命令进行链接:

g++ -o sum-parallel sum-parallel.c -fopenmp