gpt4 book ai didi

C - 未引用的 Omp 函数

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:22 24 4
gpt4 key购买 nike

我正在使用 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 的 undefined reference 和所有其他 Omp 函数。为什么?

最佳答案

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

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

关于C - 未引用的 Omp 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43914332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com