2017-05-11 44 views
0

我正在研究使用Omp的'C'並行編程。在我的模塊陣列求和parallel.c我包括第一,作爲請求,文件hpc.h是在我的C文件相同的文件夾,幷包含以下代碼:C - Unreferenced Omp函數

/*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鏈接我得到這個錯誤: 陣列 - sum-parallel.c :(.text + 0x9):未定義的引用omp_set_num_threads和所有其他Omp函數。爲什麼?

+2

的'-c'標誌'gcc'只做編譯階段,輸出文件尚未鏈接。最簡單的解決方案,如果你想編譯和鏈接在一個步驟是省略'-c' –

+2

我100%確定你不能*執行*你從任何平臺上'gcc -c -Wall -Wpedantic -fopenmp'獲得的文件。你的意思是當你試圖鏈接它嗎? –

+0

@AjayBrahmakshatriya是的,我的意思是鏈接抱歉。我編輯了這個問題。 – Caramelleamare

回答

1

你有你的編譯後的文件與OMP庫鏈接,你可以用G ++編譯和單命令鏈接:

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

我編輯的問題,因爲問題是在鏈接階段,而不是顯然可執行文件。你的代碼可以工作,就像'gcc -c ...'之後的'gcc -o array-sum array-sum-parallel.o fopenmp'一樣。爲什麼_fopenmp_需要成爲最後的標誌? – Caramelleamare

+1

它可能是第一個標誌,沒有限制 –

+0

但是這樣'gcc -o -fopenmp array-sum array-sum-parallel.o'告訴我文件'array-sum'不存在。 這樣''gcc -o array-sum array-sum-parallel.o -fopenmp'沒關係。 – Caramelleamare