glc78
glc78

Reputation: 439

C - Unreferenced Omp function

I'm working on 'C' parallel programming for my studies using Omp. In my module array-sum-parallel.c I include as first, as requested, the file hpc.h that is in the same folder of my C file and contains the following code:

/*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

Then I include omp.h, stdio.h, stdlib.h, I compile with gcc -c -Wall -Wpedantic -fopenmp and it's ok, but when I link with gcc -o -fopenmp array-sum-parallel array-sum-parallel.o I get this error: array-sum-parallel.c:(.text+0x9): undefined reference to omp_set_num_threads and all the others Omp functions. Why?

Upvotes: 0

Views: 734

Answers (1)

e.jahandar
e.jahandar

Reputation: 1763

You have to link your compiled files with omp library, you can use g++ to compile and link with single command :

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

Upvotes: 2

Related Questions